struct // anonymous struct type
{
int date;
char *name;
} test;
위의 문은 익명 구조체 유형을 정의하고 이름이없는이 구조체 형식의 변수 test
을 만듭니다. 마찬가지로, 아래 문은 익명 구조체 유형을 정의하고이 유형의 변수 massage
을 생성 -
struct // anonymous struct type
{
void *check;
} massage;
타입 변환 연산자는 괄호 (type)
아닌 변수 이름의 유형이 있어야합니다. 따라서 타입 변환 연산자를 사용하려면 먼저 struct
에 이름 (태그)을 지정해야합니다. 또한 타입 변환 연산자의 결과는 r-value
이므로 멤버 선택 .(dot)
연산자 (멤버의 이름이어야 함)와 함께 사용할 수 없습니다. 따라서 구조체에서 값을 가져온 후에는 형변환 연산자를 적용해야합니다. 따라서, 다음과 같은 표현은 잘못된 것입니다 -
massage.((struct foo *)check)->date
// |____________________|
// |
// this should be the member name but it
// evaluates to a r-value - the result of
// the typecast operator assuming struct tag
// is foo
// it should instead be
((struct foo *)massage.check)->date
// dot operator has higher precedence than typecast
// so the struct member check is fetched first and
// it is typecast to type (struct foo *)
나는 다음과 같은 변경을 제안 - 여기
// standard headers should be
// enclosed in angle < > brackets
#include <stdio.h>
// give the structure a name so it can be
// used in typecasting
struct foo {
int date;
char *name;
} test;
// anonymous struct type
struct {
void *check;
} massage;
// return type of main should be int and
// parameter list should contain void
int main(void) {
test.date = 21;
test.name = "Nilesh";
massage.check = &test;
// fetch the struct member check and then
// apply typecast operator
printf("date - %d , name - %s\n", ((struct foo *)massage.check)->date,
((struct foo *)massage.check)->name);
return 0;
}
감사합니다. 나는 내가 원하는 것을 정확하게 얻었다. – user3452214
Ajay 이것은 10 점 만점에 매우 좋은 설명입니다. –