Wayne Dawson on Sun, 8 Dec 2002 20:03:06 -0500 |
On Sun, 8 Dec 2002, Jeff Abrahamson wrote: > If I change the code snippet to this: > > int foo(struct bar *b); > foo(struct bar *b) { return 0; } > > I still get an error: > > jeff@diderot:scratch $ gcc -c struct2.c > struct2.c:1: warning: `struct bar' declared inside parameter list > struct2.c:1: warning: its scope is only this definition or declaration, which is probably not what you want. > struct2.c:2: warning: `struct bar' declared inside parameter list > struct2.c:2: conflicting types for `foo' > struct2.c:1: previous declaration of `foo' > jeff@diderot:scratch $ > > Note that gcc is complaining of conflicting types for foo, which is an > error, and no .o file is generated. It's also complaining, first, that you're declaring 'struct bar' within the parameter list. > My read of K&R is that, as long as you don't need to know the size of > the struct pointer, you don't need to have the definition of the > struct available in the current translation unit. The problem isn't with the pointer. gcc thinks you're declaring a struct within the parameter list. And such a declaration has a very limited scope indeed! > The real issue here, since I could just #include bar.h, is that I > don't want to #include extraneous files. It slows compilations, risks > breaking data abstraction, and risks creating circular dependencies. Why not just put your declaration for Bar before the declaration of foo()? It doesn't have to be in an include file. The following program works: typedef struct bar { int a; } Bar; int foo(Bar b); /* note: this forward declaration isn't necessary */ int foo(Bar b) { return b.a; } main() { Bar x = { 37 }; printf("foo() returns %d\n",foo(x)); } But the following program produces a similar error to the one you got: int foo(struct bar b); typedef struct bar { int a; } Bar; int foo(Bar b) { return b.a; } main() { Bar x = { 37 }; printf("foo() returns %d\n",foo(x)); } Even the following program produces a similar error: int foo(struct bar b); typedef struct bar { int a; }; int foo(struct bar b) { return b.a; } main() { struct bar x = { 37 }; printf("foo() returns %d\n",foo(x)); } Wayne _________________________________________________________________________ Philadelphia Linux Users Group -- http://www.phillylinux.org Announcements - http://lists.netisland.net/mailman/listinfo/plug-announce General Discussion -- http://lists.netisland.net/mailman/listinfo/plug
|
|