简体   繁体   English

动态更改指向结构的指针数组的数据类型

[英]Dynamically change the data type of an array of pointers to a struct

I'm trying to code an array of pointers of variables from a struct.我正在尝试从结构中编写变量指针数组。 My problem is that the variables inside the struct have different data type.我的问题是结构内的变量具有不同的数据类型。

#include "stdlib.h"

typedef struct val {
 unsigned char a;
 unsigned char b;
 unsigned short c;
 unsigned int d;
} _letters;

void setup() {
 Serial.begin(9600);
}

int var1 = 0;

void loop() {
 _letters lt;
 lt.a = 1;
 lt.b = 2;
 lt.c = 3;
 lt.d = 4;

 unsigned char *ptrLetters[4];
 ptrLetters[0] = &lt.a;
 ptrLetters[1] = &lt.b;
 ptrLetters[2] = &lt.c;   //here is the problem
 ptrLetters[3] = &lt.d;  //also here

 var1 = (int)*ptrLetters[0];

 Serial.println(var1);
}

The purpose of this is because I want to save the address and access the variables from the struct (which I CAN'T modify) by the index of the array (*ptrLetters[index]), but the problem is that inside the struct there are different data types and the pointer is initialised only for char types.这样做的目的是因为我想通过数组的索引 (*ptrLetters[index]) 保存地址并访问结构中的变量(我无法修改),但问题是在结构内部是不同的数据类型,指针仅针对 char 类型初始化。 How do I dynamically change that?我如何动态改变它?

Pointer to object of any type can be implicitly converted to pointer to void (optionally cv-qualified);指向任何类型对象的指针都可以隐式转换为指向 void 的指针(可选 cv 限定); the pointer value is unchanged.指针值不变。 The reverse conversion, which requires static_cast or explicit cast, yields the original pointer value:需要static_cast或显式static_cast转换的反向转换产生原始指针值:

void *ptrLetters[4];
ptrLetters[0] = &lt.a;
ptrLetters[1] = &lt.b;
ptrLetters[2] = &lt.c;   //no more problem
ptrLetters[3] = &lt.d;  //no more problem here as well

For dereferencing, in C you can simply do this:对于取消引用,在 C 中,您可以简单地执行以下操作:

unsigned char var1 = *((char*)ptrLetters[0]);
unsigned char var2 = *((char*)ptrLetters[1]);
unsigned short var3 = *((unsigned short*)ptrLetters[2]);
unsigned int var4 = *((unsigned int*)ptrLetters[3]);

Since you tagged this with C++ also, it is better to use static_cast in C++.由于您也用 C++ 标记了它,因此最好在 C++ 中使用static_cast

unsigned char var1 = *(static_cast<unsigned char*>(ptrLetters[0]));
unsigned char var2 = *(static_cast<unsigned char*>(ptrLetters[1]));
unsigned short var3 = *(static_cast<unsigned short*>(ptrLetters[2]));
unsigned int var4 = *(static_cast<unsigned int*>(ptrLetters[3]));

You can use an array of void (untyped) pointers:您可以使用 void(无类型)指针数组:

void *ptrLetters[4];
ptrLetters[0] = &lt.a;
ptrLetters[1] = &lt.b;
ptrLetters[2] = &lt.c;   //here is the problem
ptrLetters[3] = &lt.d;  //also here

And you can then access your variables like this:然后您可以像这样访问您的变量:

var1 = *((char*)ptrLetters[0]);

Note that you have to cast the void pointers back to the original type first for dereferencing.请注意,您必须首先将 void 指针强制转换回原始类型才能取消引用。 The conversion to int is then performed implicitly on assignment.然后在赋值时隐式执行到 int 的转换。

You've not explained much on why you want to use only array of pointers or why you want to use pointers at all.您还没有解释为什么只想使用指针数组或为什么要使用指针。 Based upon what you're trying to do in your sample code, you can do it many ways.根据您在示例代码中尝试执行的操作,您可以通过多种方式进行操作。

The simplest would be to use a pointer to struct:最简单的方法是使用指向结构的指针:

_letters *ptrLetter;
ptrLetter = &lt;

var1 = ptrLetter->a;

You can also explore struct of pointers or use C++ classes to contain the data and pointer together.您还可以探索指针结构或使用 C++ 类来包含数据和指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM