Is the following code legal C?
#include <stdio.h>
typedef struct _BASE_STRUCT
{
int BaseMember;
} BASE_STRUCT, *PBASE_STRUCT;
typedef struct _DERIVED_STRUCT
{
BASE_STRUCT; // Members belonging to this struct are "embedded" here.
int DerivedMember;
} DERIVED_STRUCT, *PDERIVED_STRUCT;
//
// Above struct declaration is equivalent to the following, which I believe is valid
// in C11 (anonymous structs).
//
// typedef struct _DERIVED_STRUCT
// {
// struct
// {
// int BaseMember;
// };
// int DerivedMember;
// } DERIVED_STRUCT, *PDERIVED_STRUCT;
//
int main()
{
DERIVED_STRUCT ds;
ds.BaseMember = 10; // Can be accessed without additional indirection.
ds.DerivedMember = 20;
printf("%d\n", ds.BaseMember);
return 0;
}
Visual Studio doesn't seem to complain about it, except for the warning about anonymous structs. However, it has the same warning for code that uses anonymous structs proper, so I assume it just hasn't been updated to be C11-conforming yet.