How can I return an anonymous struct in C?

Askaga picture Askaga · Jan 9, 2015 · Viewed 9k times · Source

Trying some code, I realized that the following code compiles:

struct { int x, y; } foo(void) {
}

It seems as if we are defining a function named foo which returns an anonymous struct.

Does it only happen to compile with my compiler or is this legal C(99)?

If so, what is the correct syntax for a return statement and how can I correctly assign the returned value to a variable?

Answer

2501 picture 2501 · Jan 9, 2015

The struct you're returning is not an anonymous struct. The C standard defines an anonymous struct as a member of another struct that doesn't use a tag. What you're returning is a struct without a tag, but since it isn't a member, it is not anonymous. GCC uses the name < anonymous > to indicate a struct without a tag.

Let's say you try to declare an identical struct in the function.

struct { int x, y; } foo( void )
{
    return ( struct { int x, y; } ){ 0 } ;
}

GCC complains about it: incompatible types when returning type 'struct < anonymous>', but 'struct <anonymous>' was expected.

Apparently the types are not compatible. Looking in the standard we see that:

6.2.7 Compatible type and composite type

1: Two types have compatible type if their types are the same. Additional rules for determining whether two types are compatible are described in 6.7.2 for type specifiers, in 6.7.3 for type qualifiers, and in 6.7.6 for declarators. Moreover, two structure, union, or enumerated types declared in separate translation units are compatible if their tags and members satisfy the following requirements: If one is declared with a tag, the other shall be declared with the same tag. If both are completed anywhere within their respective translation units, then the following additional requirements apply: there shall be a one-to-one correspondence between their members such that each pair of corresponding members are declared with compatible types; if one member of the pair is declared with an alignment specifier, the other is declared with an equivalent alignment specifier; and if one member of the pair is declared with a name, the other is declared with the same name. For two structures, corresponding members shall be declared in the same order. For two structures or unions, corresponding bit-fields shall have the same widths. For two enumerations, corresponding members shall have the same values.

The second bold part, explains that if both struct are without the tag, such as in this example, they have to follow additional requirements listed following that part, which they do. But if you notice the first bold part, they have to be in separate translation units, and structs in the example aren't. So they are not compatible and the code is not valid.

It is impossible to make the code correct since if you declare a struct and use it in this function. You have to use a tag, which violates the rule that both have structs have to have the same tag:

struct t { int x, y; } ;

struct { int x, y; } foo( void )
{
    struct t var = { 0 } ;

    return var ;
}

Again GCC complains: incompatible types when returning type 'struct t' but 'struct <anonymous>' was expected