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

  2. j0mbolar <> wrote:
    > 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.

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


    --
    <Insert your favourite quote here.>
    Erik Trulsson
     
    Erik Trulsson, Oct 16, 2004
    #2
    1. Advertising

  3. j0mbolar

    Pedro Graca Guest

    j0mbolar wrote:
    > 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 :)


    --
    USENET would be a better place if everybody read:
    http://www.expita.com/nomime.html
    http://www.netmeister.org/news/learn2quote2.html
    http://www.catb.org/~esr/faqs/smart-questions.html
     
    Pedro Graca, Oct 16, 2004
    #3
  4. j0mbolar

    Neil Kurzman Guest

    j0mbolar wrote:

    > 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.
     
    Neil Kurzman, Oct 17, 2004
    #4
  5. j0mbolar

    Chris Torek Guest

    In article <news:>
    j0mbolar <> wrote:
    [gcc says]
    >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?


    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. :)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Oct 17, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Goche
    Replies:
    1
    Views:
    851
    Ranganath Kini
    Feb 9, 2006
  2. Dimitris Mandalidis

    Re: malloc & incompatible types in assignment

    Dimitris Mandalidis, Aug 31, 2003, in forum: C Programming
    Replies:
    4
    Views:
    1,039
    Al Bowers
    Aug 31, 2003
  3. Brian Stubblefield

    incompatible types in assignment

    Brian Stubblefield, May 25, 2004, in forum: C Programming
    Replies:
    7
    Views:
    33,799
    Michael Fyles
    May 30, 2004
  4. Dennis Schulz

    error: incompatible types in asignment

    Dennis Schulz, May 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    497
    Emmanuel Delahaye
    May 26, 2004
  5. Replies:
    3
    Views:
    888
Loading...

Share This Page