Re: Weird gcc behaviour with function pointer types



On Mar 29, 3:05 am, greg <g...@xxxxxxxxxxxxxxxxxxxxx> wrote:
In my quest to eliminate C compiler warnings from
Pyrex output, I've discovered some utterly bizarre
behaviour from gcc 3.3.

The following code:

void g(struct foo *x) {
}

void f(void) {
void (*h)(struct foo *);
h = g;
}

produces the following warning:

blarg.c: In function `f':
blarg.c:6: warning: assignment from incompatible pointer type

However, adding the following line at the top:

typedef struct foo Foo;

makes the warning go away. The mere *presence* of
the typedef is all that's needed -- it doesn't even
have to be used.

This looks like a bug in gcc to me -- what do people
think?

--
Greg

If there is no outer declaration of struct foo visible to both
functions
gcc is right (it usually is when it comes to C technicalities). The
scope
of a struct declared inside a parameter list is limited to the
parameter
list itself, so the two "struct foo"s are technically different types.
Adding the typedef provides a common declaration of struct foo that is
shared by both functions. You don't really even need a typedef; a
global
declaration of just "struct foo;" should make the problem go away.

I would have expected gcc to also warn you about declaring a struct
inside
a parameter list. Did you get any warnings like that?

.



Relevant Pages

  • Re: problem in modular programing
    ... the prototype against the declaration: while I can see that this ... But consider this gcc example: ... void foo ... And foo the foo function will be reading garbage it seems. ...
    (comp.lang.c)
  • Re: error : $S1 : modifiers not allowed ?!
    ... File a bug report on Connect. ... operator Foo() const; ... typedef struct Foo ... foo.cpp: see declaration of 'Foo' ...
    (microsoft.public.vc.language)
  • error : $S1 : modifiers not allowed ?!
    ... does not support default-int ... operator Foo() const; ... typedef struct Foo ... foo.cpp: see declaration of 'Foo' ...
    (microsoft.public.vc.language)
  • Re: variable declaration and definition!
    ... declaration and definition.it would be very helpful if anyone out ... is a declaration of an aggregate type called foo. ... proper declaration before they can be used. ...
    (comp.lang.c)
  • forward declaration error
    ... invalid use of undefined type `struct foo' ... I believe this is because the forward declaration of foo doesn't actually ...
    (alt.comp.lang.learn.c-cpp)