0

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:

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):

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?

Juan Perez
  • 23
  • 2
  • You mean processes (not threads) by "process"? If so, modern OS separates address spaces for each processes, so you will have to use special functions to modify memory in other processes. – MikeCAT Sep 02 '20 at 12:25
  • @MikeCAT `Serial.print` looks like Arduino, it might allow that (I don't know if it does or not). – Yksisarvinen Sep 02 '20 at 12:27
  • 1
    `(void*)(val1 + x);` — Are you trying to convert an integer into a pointer? I don't think this is a good idea, at least if you don't use something `uintptr_t`. Even then, it may be very fragile to use such a pointer not to cause undefined behavior. – Daniel Langr Sep 02 '20 at 12:36
  • If you get a compilation error, as the title suggests, how can the values be anything at all? – molbdnilo Sep 02 '20 at 13:37

2 Answers2

0

You not change val1 and val2 with this code. You just affect new pointer allocation in values

values[0] = (void*)(val1 + x);
values[2] = (void*)(val2 + x);
Zeppi
  • 1,175
  • 6
  • 11
0

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

dark_prince
  • 237
  • 2
  • 12