【发布时间】:2021-09-13 13:48:31
【问题描述】:
我正在尝试从结构指针数组中访问指针索引值:
typedef struct
{
bool status;
uint8_t sensnr;
uint8_t set_vallue;
uint8_t actuval_value;
} temp_t;
//array of struct
temp_t temp[5];
typedef struct
{
uint8_t senspwrsource;
uint8_t sensnr;
uint8_t max_value;
} sensor_t;
//array of structs
sensor_t sens[5];
typedef union {
temp_t temp;
sensor_t sensor;
} temp_sens_t;
typedef struct
{
bool status;
struct temp_sens_t *temp_value[3];
struct temp_sens_t *sensors[3];
char sensor_name[MAX_LEN];
} tempsensors_t;
//array of structures
tempsensors_t sensors[5];
我能够分配所有值,但我无法从结构指针“temp_value[3]”中读取值。
到目前为止,我已经尝试过这样的:
temp_t *tempinfo;
tempinfo = &sensors[1]->temp_value[0];
//trying to access values like this but not working.
teminfo.sensnr ;
如何访问索引为 [0 到 2] 的结构指针数组中的值?
【问题讨论】:
-
tempinfo->sensor? -
sensor中的tempinfo = &sensor[1]->temp_value[0];是什么?请出示minimal reproducible example。 -
@verendra 请不要描述您尝试过的内容,而是edit yopur 问题并显示您作为minimal reproducible example 尝试过的内容
-
关于:我能够分配所有值,但我无法从 C 中的结构指针 "temp_value[3]" 读取值,索引是基于 0 的,所以访问
temp_value[3];正在尝试访问超出数组的末尾
标签: arrays c pointers structure