pointers to incompatible types

Discussion in 'C Programming' started by j0mbolar, Oct 16, 2004.

  1. j0mbolar

    j0mbolar Guest

    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?
     
    j0mbolar, Oct 16, 2004
    #1
    1. Advertisements

  2. The compiler is correct.
    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.
     
    Erik Trulsson, Oct 16, 2004
    #2
    1. Advertisements

  3. j0mbolar

    Pedro Graca Guest

    struct bar; /* incomplete definition */
    [snip rest of code]

    [snip]
    '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 [???]).
    I don't think so :)
     
    Pedro Graca, Oct 16, 2004
    #3
  4. j0mbolar

    Neil Kurzman Guest

    You must define the structure ( or any types) before you can use it.
     
    Neil Kurzman, Oct 17, 2004
    #4
  5. j0mbolar

    Chris Torek Guest

    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. :)
     
    Chris Torek, Oct 17, 2004
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.