Making an array element a reference to another variable

Hello. I have an array of strings. I need one of the elements to be always equal to another variable outside of the array. I've tried to do this with pointers, but have had no luck. Thanks in advance.

prog_char  str1[] PROGMEM = "string 1";
prog_char str2[] PROGMEM = ".string2";

PROGMEM const char *Strs[] = 	  
{str1,str2};

// to extract you then use

    strcpy_P(Buffer, (char*)pgm_read_word(&(Strs[i])));

Is the kind of thing you are after?

Happy Coding 8) 8)

I read it differently. I thought he is asking for an array of pointers to other variables. For example:

void setup() {
  //Initialize serial and wait for port to open:\
int *ptr[10];
int i;
int a, b, c;

Serial.begin(115200);
a = 5;
b = 10; 
c = 15;

ptr[0] = &a;
ptr[1] = &b;
ptr[2] = &c;
for (i = 0; i < 3; i++)
  Serial.println(*ptr[i]);
}

You may be right ..

But thats given two examples of arrays using pointers..

int array [12];
int &x = array [2];

econjack:
I read it differently. I thought he is asking for an array of pointers to other variables. For example:

Right, but I need only one element to be a pointer. Is that possible?

AWOL,

String menuItems[] = {"item1", "item2", "temp", "item4"};
String str = "item3";
&str = menuItems[2];

doesn't compile:
error: lvalue required as left operand of assignment

&str = menuItems[2];

error: lvalue required as left operand of assignment

That's perfectly correct - you can't assign a value to a constant address.

Some people don't know what lvalue or rvalue means. Think of a diagram with an large inverted "V", with the name of the variable at the point of the V. The leg pointing to the left is the lvalue, which is the memory address of where that variable is located in memory. The term lvalue probably comes from the assembly language days when a memory address was called its "location value", or lvalue.

The right leg of the V is what is stored at that memory location. This, too, likely comes from assembly language and stood for "register value", or rvalue.

Now take the definition of a variable:

int x;

and think of it as a bucket. The lvalue tells you where the bucket is located in memory, the rvalue tells you what's inside the bucket, and the data type specifier (e.g., the int keyword) tells you the size of the bucket...big enough to hold two bytes in this case. I developed this into The Bucket Analogy for my books, as it makes it easier to explain casts and pointers. For example:

int x;
float y = 35000;

x = y;

is a bad assignment because you're taking the contents of a 4 byte bucket and trying to pour it into a 2 byte bucket. You run the risk of overflowing and losing data. Changing the assignment to

x = (int) y;

using a cast is the correct thing to do even when the compiler doesn't flag it as an error.

Probably an H-Bomb-kill-an-ant post, but perhaps helps to explain what you're seeing.

On the other hand, take a look at reply #4, which does work.

True enough, the code:

void setup() {
  Serial.begin(115200);
   int array [12];
   int &x = array [2];

}

does compile, but the code:

void setup() {
  Serial.begin(115200);
   int array [12];
   int &x = array [2];

   &x = array [2];
}

doesn’t. The syntax works in the assignment statement because you are fixing the allocated memory address at the time of its definition. The last statement won’t work because you are trying to change the lvalue of the variable after its been assigned. While your statement works in the assignment, I have difficulty seeing a valid use for that syntax.

While your statement works in the assignment, I have difficulty seeing a valid use for that syntax.

Me neither, but it is an example of a reference to an array element, which is the closest approach yet to an answer to the original question - if the array element changes, the reference changes, and vice versa.

Glebun:
Hello. I have an array of strings. I need one of the elements to be always equal to another variable outside of the array. I've tried to do this with pointers, but have had no luck. Thanks in advance.

You could access the strings through a function that would access the outside variable as needed.

The "always be equal" part... you have to make that happen unless you use pointers and even then depending on what you mean, your code might have to "make equal".