簡體   English   中英

如何訪問結構的元素

[英]How to access the elements of a structure

在以下代碼中,如何訪問結構 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;

請幫我。 提前致謝。

使用點運算符訪問結構成員。
對於指針變量,請使用 -> 運算符。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM