pointers to incompatible types

J

j0mbolar

What is the chapter and verse for the following?

#include <stdio.h>

void foo(struct bar *baz);

struct bar {
int x, y;
};

int main(void)
{
return 0;
}


Which, with gcc, yields:

dec.c:3: warning: `struct bar' declared inside parameter list
dec.c:3: warning: its scope is only this definition or declaration,
which is probably not what you want.


Is it just me or is this compiler stupid?
Where is struct bar declared inside the parameter list?

It would make sense if it was instead:
void foo(struct bar { int x, y; } * baz);

or something equally insane but this isn't the case.
the type definition follows /after/ the prototype
and additionally we can have pointers to
incomplete types. So this is a case of gcc
issuing an incorrect diagnostic, yes?
 
E

Erik Trulsson

j0mbolar said:
What is the chapter and verse for the following?

#include <stdio.h>

void foo(struct bar *baz);

struct bar {
int x, y;
};

int main(void)
{
return 0;
}


Which, with gcc, yields:

dec.c:3: warning: `struct bar' declared inside parameter list
dec.c:3: warning: its scope is only this definition or declaration,
which is probably not what you want.


Is it just me or is this compiler stupid?

The compiler is correct.
Where is struct bar declared inside the parameter list?

In the line

void foo(struct bar *baz);

Since the compiler has not seen any declaration of 'struct bar'
previously this is treated as a forward declaration of 'struct bar'
*which is only valid inside this function declaration/definition.*

When the compiler reaches your declaration of 'struct bar' further down
it treats this as a declaration of a new type (since the scope of the
previous declaration was only that function.)

I.e. the two references you have to 'struct bar' refers to *different*
types which just happen to have the same name.
 
P

Pedro Graca

j0mbolar said:
What is the chapter and verse for the following?

#include <stdio.h>

struct bar; /* incomplete definition */
void foo(struct bar *baz);
[snip rest of code]

[snip]
... we can have pointers to
incomplete types.

'struct bar' is *not* a incomplete type at
the time foo() is parsed.

It's a nonexistent type (and gcc tries to
define it there and then [???]).
So this is a case of gcc
issuing an incorrect diagnostic, yes?

I don't think so :)
 
N

Neil Kurzman

j0mbolar said:
What is the chapter and verse for the following?

#include <stdio.h>

void foo(struct bar *baz);

struct bar {
int x, y;
};

int main(void)
{
return 0;
}

Which, with gcc, yields:

dec.c:3: warning: `struct bar' declared inside parameter list
dec.c:3: warning: its scope is only this definition or declaration,
which is probably not what you want.

Is it just me or is this compiler stupid?
Where is struct bar declared inside the parameter list?

It would make sense if it was instead:
void foo(struct bar { int x, y; } * baz);

or something equally insane but this isn't the case.
the type definition follows /after/ the prototype
and additionally we can have pointers to
incomplete types. So this is a case of gcc
issuing an incorrect diagnostic, yes?

You must define the structure ( or any types) before you can use it.
 
C

Chris Torek

dec.c:3: warning: `struct bar' declared inside parameter list
dec.c:3: warning: its scope is only this definition or declaration,
which is probably not what you want.

Is it just me or is this compiler stupid?[/QUOTE]

As far as I have seen, you have gotten one right answer and several
completely wrong answers to this Frequently Asked Question (#11.5).

The FAQ has the correct answer. Note that if you had looked there
first, you would have gotten a good answer quickly, instead of one
good answer and at least two bad ones somewhat less quickly. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top