Jeff Abrahamson on Sun, 8 Dec 2002 18:51:05 -0500


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] C question: struct pointer error


On Sun, Dec 08, 2002 at 06:11:02PM -0500, Walt Mankowski wrote:
> On Sun, Dec 08, 2002 at 04:59:59PM -0500, Jeff Abrahamson wrote:
> > I wonder if someone can help me see what I'm missing.
> > 
> > The following code snippet yields a warning and an error. I don't
> > think it should. Am I confused, or is gcc?
> 
> You haven't done anything really *wrong*, it's just gcc being strict
> because you told it to give all warnings.
> 
> >     int foo(struct bar *b);
> > 
> >     typedef struct bar {
> > 	    int a;
> >     } Bar;
> > 
> >     foo(Bar *b)
> >     {
> > 	    return 0;
> >     }
> 
> A typedef isn't a #define.  Even though here they're different names
> for the same structure, gcc warns that they're different.  To get rid
> of the warning, either change the first line to [...]

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.

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 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.

-- 
 Jeff

 Jeff Abrahamson  <http://www.purple.com/jeff/>
 GPG fingerprint: 1A1A BA95 D082 A558 A276  63C6 16BF 8C4C 0D1D AE4B

Attachment: pgpWxT8xnsCNi.pgp
Description: PGP signature