如何访问结构的元素

在以下代码中,如何访问结构details_1和details_2的元素?

typedef struct{
    unsigned char student;
    unsigned int roll_no;
}details_1;

typedef struct{
    unsigned long pin_code;
    unsigned char birthdate;
}details_2;

typedef union{
    details_1  COUNT8;
    details_2  COUNT16;
}details_union;

请帮助我。 预先感谢。

yanbinfighting 回答:如何访问结构的元素

使用点运算符访问结构成员。
对于指针变量,请使用->运算符。

details_1 d1 = {'c',1};
details_2 d2 = {999999,'b'};
details_union du = {d1};
printf ("Access student directly: %c\n",d1.student);
printf ("Access student through union: %c\n",du.COUNT8.student);
printf ("Access pin_code through union: %lu\n\n",du.COUNT16.pin_code);  // not this value

du.COUNT16=d2;
printf ("Access pin_code directly: %lu\n",d2.pin_code);
printf ("Access pin_code through union: %lu\n",du.COUNT16.pin_code);
printf ("Access student through union: %c\n",du.COUNT8.student); // not this value
本文链接:https://www.f2er.com/2621896.html

大家都在问