結構體巢狀及定義方式

2021-10-05 23:29:16 字數 988 閱讀 9158

1,結構體定義一

定義:typedef struct

gpio_struct;

typedef struct

nixie_tube_struct;

申明結構體變數:

nixie_tube_struct nixie_tube_handler = ;

成員變數初始化:

nixie_tube_handler.sda.port_x = gpio2;

nixie_tube_handler.sda.gpio_x = gpio_pin_3;

nixie_tube_handler.scl.port_x = gpio2;

nixie_tube_handler.scl.gpio_x = gpio_pin_2;

2,結構體定義二

typedef struct

gpio_struct;

typedef struct

nixie_tube_struct;

申明結構體變數:

nixie_tube_struct nixie_tube_handler = ;

成員變數初始化:

//結構體指標所指向的結構體,需要分配空間

nixie_tube_handler.sda = (gpio_struct*)malloc(sizeof(gpio_struct));

nixie_tube_handler.scl = (gpio_struct*)malloc(sizeof(gpio_struct));

nixie_tube_handler.sda->port_x = gpio2;

nixie_tube_handler.sda->gpio_x = gpio_pin_3;

nixie_tube_handler.scl->port_x = gpio2;

nixie_tube_handler.scl->gpio_x = gpio_pin_2;

結構體中的指標成員變數所指向的結構體需要分配位址及空間,指標只是個位址

結構體巢狀結構體名

結構體巢狀結構體名 前一段時間在看ddk中例子的時候,看到這樣的的結構體定義 typedef struct common device data common device data,pcommon device data typedef struct pdo device data pdo dev...

結構體中巢狀結構體

結構體的巢狀問題 結構體的自引用 self reference 就是在結構體內部,包含指向自身型別結構體的指標。結構體的相互引用 mutual reference 就是說在多個結構體中,都包含指向其他結構體的指標。1.1不使用typedef時 錯誤的方式 struct tag 1 這種宣告是錯誤的,...

Go結構體巢狀

package main import fmt 結構體巢狀 結構體巢狀 乙個結構體中的字段,是另乙個結構體型別 has a func main b1.bookname 西遊記 b1.price 45.8 s1 student s1.name 小明 s1.age 21 s1.book b1 fmt.p...