Declaration of structs inside parameter list

Discussion in 'C Programming' started by Michael Birkmose, May 10, 2004.

  1. Hi,

    Using gcc the following is possible:

    int some_function(struct local_struct { int member;} a);

    This function takes one parameter "a" of the type struct local_struct.
    This type is declared locally on the paramter list, and only has scope in
    that function.

    Gcc gives the following output:

    warning: structure defined inside parms
    warning: `struct local_struct' declared inside parameter list
    warning: its scope is only this definition or declaration, which is probably not what you want.

    My question is now - how would anyone invoke this function with such a
    parameter? I mean it's hard to instantiate a variable of that type, since
    it is not declared outside the function?

    --
    Michael Birkmose - stud.polyt
    Aalborg University - Department of Computer Science
    Fredrik Bajers Vej 7, B1-215
    Michael Birkmose, May 10, 2004
    #1
    1. Advertising

  2. Michael Birkmose

    Kevin Bracey Guest

    In message <>
    Michael Birkmose <> wrote:

    > int some_function(struct local_struct { int member;} a);
    >
    > This function takes one parameter "a" of the type struct local_struct.
    > This type is declared locally on the paramter list, and only has scope in
    > that function.
    >
    > My question is now - how would anyone invoke this function with such a
    > parameter? I mean it's hard to instantiate a variable of that type, since
    > it is not declared outside the function?


    You can't, which is why gcc is warning you that it's probably not what you
    want.

    --
    Kevin Bracey, Principal Software Engineer
    Tematic Ltd Tel: +44 (0) 1223 503464
    182-190 Newmarket Road Fax: +44 (0) 1223 503458
    Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
    Kevin Bracey, May 10, 2004
    #2
    1. Advertising

  3. Michael Birkmose

    SM Ryan Guest

    Michael Birkmose <> wrote:
    # Hi,
    #
    # Using gcc the following is possible:
    #
    # int some_function(struct local_struct { int member;} a);

    #
    # My question is now - how would anyone invoke this function with such a
    # parameter? I mean it's hard to instantiate a variable of that type, since
    # it is not declared outside the function?

    struct another_struct {int member} x;
    some_function(x);

    This may be allowed: if the structs have the same members with
    the same types, the structs can be considerred the same by some
    compilers.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    We found a loophole; they can't keep us out anymore.
    SM Ryan, May 10, 2004
    #3
  4. SM Ryan <> writes:

    > Michael Birkmose <> wrote:
    > # Hi,
    > #
    > # Using gcc the following is possible:
    > #
    > # int some_function(struct local_struct { int member;} a);
    >
    > #
    > # My question is now - how would anyone invoke this function with such a
    > # parameter? I mean it's hard to instantiate a variable of that type, since
    > # it is not declared outside the function?
    >
    > struct another_struct {int member} x;
    > some_function(x);
    >
    > This may be allowed: if the structs have the same members with
    > the same types, the structs can be considerred the same by some
    > compilers.


    No, the structs don't have compatible types, therefore they *cannot* be
    considered the same by any conforming C implementation.

    For the definition of type compatability, see section 6.2.7 of the
    standard. 6.7.7#5 has a nice exaple:

    | EXAMPLE 2: After the declarations
    |
    | typedef struct s1 { int x; } t1, *tp1;
    | typedef struct s2 { int x; } t2, *tp2;
    |
    | type t1 and the type pointed to by tp1 are compatible. Type t1 is also
    | compatible with type struct s1, but not compatible with the types
    | struct s2, t2, the type pointed to by tp2, or int.

    Martin


    --
    ,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
    / ,- ) http://www.zero-based.org/ ((_/)o o(\_))
    \ `-' `-'(. .)`-'
    `-. Debian, a variant of the GNU operating system. \_/
    Martin Dickopp, May 10, 2004
    #4
  5. Michael Birkmose

    Mitchell Guest

    On Mon, 10 May 2004 16:29:55 +0200, Martin Dickopp
    <> wrote:

    <snip>
    >For the definition of type compatability, see section 6.2.7 of the
    >standard. 6.7.7#5 has a nice exaple:
    >
    >| EXAMPLE 2: After the declarations
    >|
    >| typedef struct s1 { int x; } t1, *tp1;
    >| typedef struct s2 { int x; } t2, *tp2;
    >|
    >| type t1 and the type pointed to by tp1 are compatible. Type t1 is also
    >| compatible with type struct s1, but not compatible with the types
    >| struct s2, t2, the type pointed to by tp2, or int.


    I'm not too familiar with type compatibility, so let me check with
    you. Are you saying that only instances of type declared at the /same/
    statement are compatible? I may have gotten my terminologies wrong..

    How about when it involved header files?

    Thanks!
    Mitchell, May 15, 2004
    #5
  6. Mitchell <> writes:

    > On Mon, 10 May 2004 16:29:55 +0200, Martin Dickopp
    > <> wrote:
    >
    > <snip>
    >>For the definition of type compatability, see section 6.2.7 of the
    >>standard. 6.7.7#5 has a nice exaple:
    >>
    >>| EXAMPLE 2: After the declarations
    >>|
    >>| typedef struct s1 { int x; } t1, *tp1;
    >>| typedef struct s2 { int x; } t2, *tp2;
    >>|
    >>| type t1 and the type pointed to by tp1 are compatible. Type t1 is also
    >>| compatible with type struct s1, but not compatible with the types
    >>| struct s2, t2, the type pointed to by tp2, or int.

    >
    > I'm not too familiar with type compatibility, so let me check with
    > you. Are you saying that only instances of type declared at the /same/
    > statement are compatible? I may have gotten my terminologies wrong..


    If two structures are declared with different tags, they don't have
    compatible types, even if their members are the same.

    > How about when it involved header files?


    That makes no difference. Preprocessing is completed before the
    implementation checks type compatability.

    Martin


    --
    ,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
    / ,- ) http://www.zero-based.org/ ((_/)o o(\_))
    \ `-' `-'(. .)`-'
    `-. Debian, a variant of the GNU operating system. \_/
    Martin Dickopp, May 17, 2004
    #6
  7. Michael Birkmose

    Chris Torek Guest

    In article <news:>
    Martin Dickopp <> writes:
    >If two structures are declared with different tags, they don't have
    >compatible types, even if their members are the same.


    This rule is new in C99 (I think -- my dead-tree edition of the C89
    standard is not easily accessible).

    I am not really sure why the rule was added, although it is certainly
    wise to use the same tag every time anyway. :)
    --
    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, May 17, 2004
    #7
  8. Michael Birkmose

    Dan Pop Guest

    In <> Chris Torek <> writes:

    >In article <news:>
    >Martin Dickopp <> writes:
    >>If two structures are declared with different tags, they don't have
    >>compatible types, even if their members are the same.

    >
    >This rule is new in C99 (I think -- my dead-tree edition of the C89
    >standard is not easily accessible).


    You're right. For C89:

    3.1.2.6 Compatible type and composite type

    Two types have compatible type if their types are the same.
    Additional rules for determining whether two types are compatible are
    described in $3.5.2 for type specifiers, in $3.5.3 for type
    qualifiers, and in $3.5.4 for declarators. Moreover, two
    structure, union, or enumeration types declared in separate
    translation units are compatible if they have the same number of
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    members, the same member names, and compatible member types; for two
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    structures, the members shall be in the same order; for two
    ^^^^^^^^^^
    enumerations, the members shall have the same values.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, May 18, 2004
    #8
    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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    618
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    543
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    629
    Eric Sosman
    Oct 11, 2005
  4. Stuart Redmann
    Replies:
    5
    Views:
    467
    Stuart Redmann
    Dec 14, 2007
  5. Luca Forlizzi
    Replies:
    4
    Views:
    442
    Luca Forlizzi
    Nov 14, 2010
Loading...

Share This Page