[英]How to access the elements of a structure
In the following code, how do I access elements of structures details_1 and details_2?在以下代码中,如何访问结构 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;
Please help me.请帮我。 Thanks in advance.
提前致谢。
Structure members are accessed using dot operator.使用点运算符访问结构成员。
For pointer variables, use the -> operator.对于指针变量,请使用 -> 运算符。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.