[LCP] Struggling with Pointers
Paul Gearon
gearon at ieee.org
Tue Jan 19 02:24:11 EST 2010
On Fri, Jan 15, 2010 at 7:50 PM, Paul Otheim <coriolis220 at gmail.com> wrote:
>
>
> On Fri, Jan 15, 2010 at 4:03 PM, Christopher Howard <choward at indicium.us>
> wrote:
>>
>> Paul Otheim wrote:
>>
>> > gcc -o insert insertEntry.c
>> > insertEntry.c:8: warning: ‘struct entry’ declared inside parameter list
>> > insertEntry.c:8: warning: its scope is only this definition or
>> > declaration, which is probably not what you want
>> > insertEntry.c: In function ‘main’:
>> > insertEntry.c:20: error: conflicting types for ‘insertEntry’
>> > insertEntry.c:8: note: previous definition of ‘insertEntry’ was here
>> >
>> >
>> > void insertEntry (struct entry *new, struct entry *here) //this is line
>> > 8
>> > {
>> > .....
>> > }
>> >
>> > int main (void)
>> > {
>> > void insertEntry (struct entry *new, struct entry *here); // this is
>> > line 20
>> >
>> > ......
>> > }
>> >
>>
>> No, the prototype for your function doesn't go inside another function.
>> (Main is a function.)
>>
>> If you have declared insertEntry() in the same file, previous to the
>> place where you intend to use it, you don't actually need a function
>> prototype. Function prototypes are for if you want to use a function
>> before you have actually defined what it does internally. Function
>> prototypes usually go in header files.
>>
>> It also appears that you have not coded a definition for "struct entry"
>> before line 8, which is confusing the compiler. See any tutorial on C
>> structs.
>>
>
> Do you hear that? It's the sound of me kicking myself!
> Thanks for your help. Just to clarify. I had declared struct entry in main()
> being under the impression for some reason that it would then be available
> to all functions.
Not only don't you want to declare your function inside of main(), you
ALSO don't want to declare the structure definition inside of main().
Both the function and the structure will be available to anything that
comes after their declarations or definitions. Do you know the
difference between definitions and declarations? Declarations are code
that tells the compiler that some function or structure exists, but
doesn't explain any of the details. These are the things that go in
header files. It's OK for a compiler to see the same declaration more
than once, so long as it is identical (a compiler may give a warning
if a declaration comes up more than once, but it's no big deal, and
there are compiler directives you can use to avoid this anyway).
Definitions describe the details of a function or structure. The main
indicator for a definition is the use of curly braces {} e.g. For a
function, the definition will include curly braces and have a "return"
statement (for non-void functions). Definitions MUST only be seen by a
compiler once.
> So if I understand correctly I have to declare it outside
> of main() which makes it a global definition. Would it then be global to all
> files linked to this file?
If it's outside of main() and you don't take steps to make it local,
then anything can refer to it, yes.
In your example:
struct entry {
// definition of entry
}
void insertEntry(struct entry *new, struct entry *here) {
// definition of insertEntry
}
int main (void) {
struct entry *here;
struct entry *new;
// get a pointer to an entry
here = get_entry(...);
// create a new entry
new = (struct entry *)malloc(sizeof(struct entry));
// now call your insertEntry function
insertEntry(new, here);
......
}
The "struct entry" needed to be declared to refer to it in the
function insertEntry, and the function insertEntry() needed to be
declared to refer to it in the function main(). (For simplicity, this
example has the full definitions, and not just declarations at the
top)
Regards,
Paul
More information about the linuxCprogramming
mailing list