0
다음 코드는 올바른 것입니까?다른 구조체에 익명 구조체로 명명 된 구조체 포함하기
#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는 익명 구조체에 대한 경고를 제외하고는 불평하지 않는 것 같습니다. 그러나 익명 구조체를 적절하게 사용하는 코드에 대해서는 동일한 경고가 있으므로 C11을 준수하도록 업데이트되지 않았다고 가정합니다.
GCC는 -fms - 확장자를 가진 그 같은 코드도지지 않습니다 표준 라이브러리). –
@JoachimPileborg 좋아, 이제 그 부분을 무시하자. :) 나는 주로'BASE_STRUCT'가 사용되는 방식을 언급하고있었습니다. – TripShock
@dxiv는 struct {}로만 구성되며, 구조체가 다른 곳에 선언되고이 예제 에서처럼 이름으로 참조되는 경우가 아닙니다. http://stackoverflow.com/questions/23527255/is-this-a-c11-anonymous-struct – arsv