Declaration of structs inside parameter list


M

Michael Birkmose

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

Advertisements

K

Kevin Bracey

In message <[email protected]>
Michael Birkmose said:
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.
 
S

SM Ryan

# 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.
 
M

Martin Dickopp

SM Ryan said:
# 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
 
M

Mitchell

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

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!
 
M

Martin Dickopp

Mitchell said:
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..

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
 
Ad

Advertisements

C

Chris Torek

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. :)
 
Ad

Advertisements

D

Dan Pop

In said:
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
 

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

Ask a Question

Top