[英]C++, “error: 'void*' is not a pointer-to-object type”
I need to create an array of pointers of different types.我需要创建一个不同类型的指针数组。 Some variables are defined at the beginning and their vectors are assigned to different spaces in the array.一些变量在开头定义,它们的向量被分配到数组中的不同空间。 Later, from another process, the values of these variables can be changed using the pointers stored in the array.稍后,在另一个进程中,可以使用存储在数组中的指针来更改这些变量的值。 Variables are accessed directly from the main process (without pointers)变量直接从主进程访问(不带指针)
In de main process at start:在 de main 进程中:
void *values[255];
uint8_t val1 = 12;
uint16_t val2 = 3478;
SetIDVariable(&val1, 0);
SetIDVariable(&val2, 2);
Others functions:其他功能:
void SetIDVariable(uint8_t *variable, uint8_t id) {
values[id] = variable;
}
void SetIDVariable(uint16_t *variable, uint8_t id) {
values[id] = variable;
}
In other process (x is a any number for check if value change):在其他过程中(x 是任意数字,用于检查值是否发生变化):
values[0] = (void*)(val1 + x);
values[2] = (void*)(val2 + x);
In main process:在主要过程中:
Serial.print("Value 1: "); Serial.println(val1);
Serial.print("Value 2: "); Serial.println(val2);
Values are always 12 and 3478. Any ideas?值总是 12 和 3478。有什么想法吗?
You not change val1 and val2 with this code.您不会使用此代码更改 val1 和 val2。 You just affect new pointer allocation in values您只会影响值中的新指针分配
values[0] = (void*)(val1 + x);
values[2] = (void*)(val2 + x);
You are converting an integer value to pointer.您正在将整数值转换为指针。 Maybe you wanted to do this:也许你想这样做:
*((uint8_t *) values[0]) = (*((uint8_t *) values[0])+x);
*((uint16_t *) values[2]) = (*((uint16_t *) values[2])+x);
also see these questions: this question and this question另请参阅这些问题: 这个问题和这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.