指向不同结构的指针数组

时间:2021-07-08 01:30:53

It is possible to do something like this How can I initialize an array of pointers to structs? but with different structs?

有可能做这样的事情如何初始化指向结构的指针数组?但结构不同?

E.g.

例如。

static struct structA_t a = {"ads", "as"};
static struct structB_t b = {"zzds", "dfr", "shywsd"};
static struct structC_t c = {"ssa", "ad", "dhksdhs"};

struct some_type *array[] = { &a, &b, &c};

How some_type will look like?

some_type会是什么样子的?

2 个解决方案

#1


8  

You could define some_type as a union:

您可以将some_type定义为联合:

typedef union{
  struct structA_t;
  struct structB_t;
  struct structC_t;
}some_type;

This will lead you to the problem that you don't know what's actually contained in which element in the array.

这将引导您解决您不知道数组中哪个元素实际包含的问题。

To overcome this, add another field specifying the content that is used:

要解决此问题,请添加另一个指定所用内容的字段:

/* numbers to identify the type of the valid some_type element */
typedef enum my_e_dataId{
  dataid_invalid = 0,
  dataid_a,
  dataid_b,
  dataid_c
} my_dataId;

typedef union u_data {
  struct structA_t* a;
  struct structB_t* b;
  struct structC_t* c;
}mydata;

typedef struct s_some_type{
  my_dataId dataId;
  mydata    myData;
}some_type;

Then you could initialize your array as follows:

然后你可以按如下方式初始化你的数组:

some_type sta[] = {
  {dataid_a, (struct structA_t*) &a},
  {dataid_b, (struct structA_t*) &b},
  {dataid_c, (struct structA_t*) &c}
};

When you loop over the elements of array, first evaluate dataId so that you know what's contained in myData. Then, for example, access the data of the first element using

循环遍历数组元素时,首先计算dataId,以便了解myData中包含的内容。然后,例如,使用访问第一个元素的数据

sta[0].myData.a->FIELDNAME_OF_A_TO_ACCESS

or the third element with

或第三个元素

sta[2].myData.c->FIELDNAME_OF_C_TO_ACCESS

See this ideone for a working example: http://ideone.com/fcjuR

有关工作示例,请参阅此ideone:http://ideone.com/fcjuR

#2


1  

In C this is possible with void pointers (Replace "struct some_type" with "void"), but you really shouldn't be doing this. Arrays are for programming with homogeneous data.

在C中,这可以使用void指针(将“struct some_type”替换为“void”),但是你真的不应该这样做。数组用于使用同类数据进行编程。

#1


8  

You could define some_type as a union:

您可以将some_type定义为联合:

typedef union{
  struct structA_t;
  struct structB_t;
  struct structC_t;
}some_type;

This will lead you to the problem that you don't know what's actually contained in which element in the array.

这将引导您解决您不知道数组中哪个元素实际包含的问题。

To overcome this, add another field specifying the content that is used:

要解决此问题,请添加另一个指定所用内容的字段:

/* numbers to identify the type of the valid some_type element */
typedef enum my_e_dataId{
  dataid_invalid = 0,
  dataid_a,
  dataid_b,
  dataid_c
} my_dataId;

typedef union u_data {
  struct structA_t* a;
  struct structB_t* b;
  struct structC_t* c;
}mydata;

typedef struct s_some_type{
  my_dataId dataId;
  mydata    myData;
}some_type;

Then you could initialize your array as follows:

然后你可以按如下方式初始化你的数组:

some_type sta[] = {
  {dataid_a, (struct structA_t*) &a},
  {dataid_b, (struct structA_t*) &b},
  {dataid_c, (struct structA_t*) &c}
};

When you loop over the elements of array, first evaluate dataId so that you know what's contained in myData. Then, for example, access the data of the first element using

循环遍历数组元素时,首先计算dataId,以便了解myData中包含的内容。然后,例如,使用访问第一个元素的数据

sta[0].myData.a->FIELDNAME_OF_A_TO_ACCESS

or the third element with

或第三个元素

sta[2].myData.c->FIELDNAME_OF_C_TO_ACCESS

See this ideone for a working example: http://ideone.com/fcjuR

有关工作示例,请参阅此ideone:http://ideone.com/fcjuR

#2


1  

In C this is possible with void pointers (Replace "struct some_type" with "void"), but you really shouldn't be doing this. Arrays are for programming with homogeneous data.

在C中,这可以使用void指针(将“struct some_type”替换为“void”),但是你真的不应该这样做。数组用于使用同类数据进行编程。