Re: Weird gcc behaviour with function pointer types



On Mar 29, 6: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?



Was there no (forward) declaration nor
definition of struct foo ahead of your functions?

I get no warnings with this:


struct foo; /* forward declaration */

void g(struct foo *x) {
}

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


Using:

Target: powerpc-apple-darwin8
Configured with: /private/var/tmp/gcc/gcc-5026.obj~19/src/configure --
disable-checking --prefix=/usr --mandir=/share/man --enable-
languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^+.-]*$/s/
$/-4.0/ --with-gxx-include-dir=/include/gcc/darwin/4.0/c++ --
build=powerpc-apple-darwin8 --host=powerpc-apple-darwin8 --
target=powerpc-apple-darwin8
Thread model: posix

gcc version 4.0.0 (Apple Computer, Inc. build 5026)

--
Hope this helps,
Steven

.



Relevant Pages

  • Re: Want comments regarding patch
    ... Then GCC is not a "reasonably good compiler". ... void bar(struct foo *a) { ... `gcc -O3 -c test.c` will generate a classic loop rather than a repz ...
    (Linux-Kernel)
  • Weird gcc behaviour with function pointer types
    ... void g{ ... void (struct foo *); ... blarg.c:6: warning: assignment from incompatible pointer type ... This looks like a bug in gcc to me -- what do people ...
    (comp.lang.python)
  • Re: Unexpected Result while compiling this code
    ... When I tried to compile that here (gcc ... > 4.0.2) it _did_ give me an error with a reference to function foo, ... > at the top of your code cause the code to compile cleanly. ... These are the warning messages i got when i compiled on gcc 3.2.2 ...
    (comp.lang.c)
  • Re: pass by Reference/value ???
    ... > void foo ... this would be called "passing by reference". ... foo receives a local copy of s and any changes that it makes to s only ... Since s is a pointer, a variable storing the address of s is a pointer to ...
    (comp.lang.cpp)
  • Re: sizeof() a function
    ... void = malloc; ... memcpy(bar, &foo, len); ... gcc doesn't seem to honor these semantics for sizeof. ... independent by using relative jumps instead of absolute jumps. ...
    (comp.os.linux.development.system)