How to modify a global value from inside an array

Hi all,

Essentially what I am trying to figure out, is how to have operations done on an element of an array reflect in the initial declaration of the elements before they were added to the array.

For instance, take this snippet of code:

double v1 = 0.0;
double v2 = 0.0;
double v3 = 0.0;

double array[] = {v1, v2, v3};

void setup() {
  Serial.println("The initial values are: ");
  Serial.print("v1 = "); Serial.println(v1);
  Serial.print("v2 = "); Serial.println(v2);
  Serial.print("v3 = "); Serial.println(v3);
  Serial.println(" \n");

  array[0] = 2.5;
  array[1] = 8.8;
  array[2] = 3.3;

  Serial.println("The new values are: ");
  Serial.print("v1 = "); Serial.println(v1);
  Serial.print("v2 = "); Serial.println(v2);
  Serial.print("v3 = "); Serial.println(v3);

}

When the values inside the array are changed, the values outside the array are not. This code output will be:

The initial values are: 
v1: 0
v2: 0
v3: 0

The new values are: 
v1: 0
v2: 0
v3: 0

What I am trying to do though is to have those initial declarations of v1, v2, and v3 be changed in accordance with what is done inside the array. I am pretty sure I am going to need to do something with the * or & operators working with pointers but I am just not quite sure how to get the desired functionality here.

Any help is greatly appreciated!
Thank you!

Right now you are just copying the values from those variables into the array. Changing the values in the array has no effect on the variables you copied the values from.

You need an array of pointers if you want to have the values in the array affect the original variables.

http://www.cplusplus.com/doc/tutorial/pointers/

double v1 = 0.0;
double v2 = 0.0;
double v3 = 0.0;

double *array[] = {&v1, &v2, &v3};

void setup() {
  Serial.println("The initial values are: ");
  Serial.print("v1 = "); Serial.println(v1);
  Serial.print("v2 = "); Serial.println(v2);
  Serial.print("v3 = "); Serial.println(v3);
  Serial.println(" \n");

  *(array[0]) = 2.5;
  *(array[1]) = 8.8;
  *(array[2]) = 3.3;

  Serial.println("The new values are: ");
  Serial.print("v1 = "); Serial.println(v1);
  Serial.print("v2 = "); Serial.println(v2);
  Serial.print("v3 = "); Serial.println(v3);

}