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, May 10, 2004
    #1
    1. Advertisements

  2. Michael Birkmose

    Kevin Bracey Guest

    In message <>
    You can't, which is why gcc is warning you that it's probably not what you
    want.
     
    Kevin Bracey, May 10, 2004
    #2
    1. Advertisements

  3. Michael Birkmose

    SM Ryan Guest

    # 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, May 10, 2004
    #3
  4. 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, May 10, 2004
    #4
  5. Michael Birkmose

    Mitchell Guest

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

    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. If two structures are declared with different tags, they don't have
    compatible types, even if their members are the same.
    That makes no difference. Preprocessing is completed before the
    implementation checks type compatability.

    Martin
     
    Martin Dickopp, May 17, 2004
    #6
  7. Michael Birkmose

    Chris Torek Guest

    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. :)
     
    Chris Torek, May 17, 2004
    #7
  8. Michael Birkmose

    Dan Pop Guest

    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, May 18, 2004
    #8
    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.