【问题标题】:Accessing values from array of struct pointers从结构指针数组访问值
【发布时间】: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


【解决方案1】:

你有(部分):

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;

但这并不意味着你认为它意味着什么。您定义的结构或联合都没有标签(如struct tag { … })。您定义了一个名为 temp_sens_t 的类型,但您确实没有定义了一个类型 struct temp_sens_t(即使您这样做了,它也与称为 temp_sens_t 的类型无关——结构标签在不同于“普通标识符”的命名空间,例如 typedef 名称)。那是“好的”——你使用的是不完整的类型。所以代码编译。但这不行,因为你不能访问不完整类型的成员。

大概,你的想法是:

typedef struct
{
  bool          status;                               
  temp_sens_t  *temp_value[3];                       
  temp_sens_t  *sensors[3];                        
  char          sensor_name[MAX_LEN]; 
} tempsensors_t;

注意结构定义中没有struct。

有了这个修改后的定义,你应该可以做到:

temp_t *tempinfo = &sensors[1]->temp_value[0]->temp;

tempinfo->sensnr = 1;

工作代码:

#include <stdbool.h>
#include <stdint.h>

enum { MAX_LEN = 32 };

typedef struct
{
  bool             status;
  uint8_t          sensnr;
  uint8_t          set_vallue;
  uint8_t          actuval_value;
} temp_t;

typedef struct
{
  uint8_t          senspwrsource;
  uint8_t          sensnr;
  uint8_t          max_value;
} sensor_t;

typedef union {
   temp_t    temp;
   sensor_t  sensor;
} temp_sens_t;

typedef struct
{
  bool          status;
  temp_sens_t  *temp_value[3];
  temp_sens_t  *sensors[3];
  char          sensor_name[MAX_LEN];
} tempsensors_t;

tempsensors_t  sensors[5];

extern void access_it(int i);

void access_it(int i)
{
    temp_t *tinfo1 = &sensors[1].temp_value[0]->temp;
    tinfo1->sensnr = i;
    temp_sens_t *tinfo2 = sensors[1].temp_value[0];
    /* Either of these works - choose the correct part of the union */
    tinfo2->sensor.sensnr = i;
    tinfo2->temp.sensnr = i;
}

注意:已咨询 GCC 10.2.0 关于此代码的准确性。

gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -fno-common -c as41.c

【讨论】:

  • 我正在尝试遍历结构数组和结构数组中的结构值。例如 temp_value 结构中的一个值需要检查结构和结构数组中的所有元素。有什么建议吗??
【解决方案2】:

就像在普通数组中一样

temp_t * tempinfo = sensors[1].temp_value[0];

因为你不是存储指向你的结构的指针,而是结构本身

正如 Andrew Henle 指出的那样,您在结构内部存储指针。

要获得他们的价值,你可以这样做

tempinfo->sensor;

点用于访问结构的成员,而箭头用于访问指向结构的指针的成员。你好像把它们调换了。

【讨论】:

  • struct temp_sens_t *temp_value[3]; 绝对是指向struct temp_sens_t 的指针 数组,而不是“结构本身”。鉴于发布的代码,这些指针永远不会被初始化并且取消引用它们是未定义的行为。
猜你喜欢
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 2022-11-30
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多