1,結構體定義一
定義:
typedef struct
{
GPIO_TypeDef* port_x; //引腳組
uint32_t gpio_x; //引腳號
}gpio_struct;
typedef struct
{
gpio_struct sda; //成員變量會有內存分配
gpio_struct scl;
uint16_t data;
}nixie_tube_struct;
申明結構體變量:
nixie_tube_struct nixie_tube_handler = {0};
成員變量初始化:
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_TypeDef* port_x; //引腳組
uint32_t gpio_x; //引腳號
}gpio_struct;
typedef struct
{
gpio_struct* sda; //指針變量,需要手動分配空間
gpio_struct* scl;
uint16_t data;
}nixie_tube_struct;
申明結構體變量:
nixie_tube_struct nixie_tube_handler = {0};
成員變量初始化:
//結構體指針所指向的結構體,需要分配空間
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;
結構體中的指針成員變量所指向的結構體需要分配地址及空間,指針只是個地址