Efficient way to re-assign char pointers to char arrays

I am dealing with char arrays and pointers to the arrays.

char firstAr[10] = "First";
char secondAr[10] = "Second";
char thirdAr[10] = "Third";

[code]char* arPtrOne = firstAr;
char* arPtrTwo = secondAr;
char* arPtrThree = thirdAr;

I also have a second set of char arrays

char fourthAr[10] = "Fourth";
char fifthAr[10] = "Fifth";
char sixthAr[10] = "Sixth";

The above char arrays and char pointers can’t be altered (I have to work with them). I need to change the char arrays that my pointers point to, so that they now point to the second set of char arrays. I know I can do this by doing the following,

arPtrOne = fourthAr;
arPtrTwo = fifthAr;
arPtrThree = sixthAr;

This is fine for my example above but if I had 100 array I would need 100 statements to point them all to new char arrays. I am hoping someone could help me find a more efficient way to do this for my example above.

To stop any duplicate effort and also help others who have the same questions discover all the relevant information, This was also posted here Efficient way to re-assign char pointers to char arrays - Stack Overflow

You might need to create an array of arrays

try experiment with something like this

char matrix[20][10] = { {... }, { ... }, {...}, ...};

You could use multi-dimensional arrays, like so:

char cArrays[3][10] = {
  "array1",
  "array2",
  "array3"
};

Then you could use an array of pointers, like so:

char *cPointers[3];
for (int i = 0; i < 3; i++) cPointers[i] = cArrays[i];

Should be quite easy to then swap individual array pointers :slight_smile:

@Danois90, thank you for the reply. I have been trying to do something very similar to what you suggested but as said above I CANNOT change the array pointers and need to point the existing char pointers to new arrays.

I can add an arrays of arrays aetc. but the result must be that my original pointers point to the new char arrays. If I use your solution I would be pointing NEW pointers to the arrays, which is not what I am trying to do. Unfortunately my problem is quite specific.

I could create the array of arrays as you suggested and store my character arrays inside but I cannot create new char array pointers. If I created the new array you suggested, would you have any ideas on how to point the char pointers I already have at the new arrays using the for loop?

If you can declare them in memory side by side there are "nasty" programming tricks with pointer magic to loop through them.

Not advised as any change (code compiler linker) can crash such adventure

@Robtillaart, thanks for the reply. I really just need to figure out a way to rotate through a list of character pointers that have not been declared in an array and declares as show above. Rotating through the new char arrays that I want to point my pointers at is not a problem because I can declare them inside an array of char arrays. But I still need some way to rotate through my original char pointers that will let me assign them to the new char arrays. Any ideas on how I could rotate through the char pointers I have? Maybe something like an array of pointers that points at the original char pointers, I'm a bit stumped.

CyberOddity:
@Robtillaart, thanks for the reply. I really just need to figure out a way to rotate through a list of character pointers that have not been declared in an array and declares as show above. Rotating through the new char arrays that I want to point my pointers at is not a problem because I can declare them inside an array of char arrays. But I still need some way to rotate through my original char pointers that will let me assign them to the new char arrays. Any ideas on how I could rotate through the char pointers I have? Maybe something like an array of pointers that points at the original char pointers, I'm a bit stumped.

sounds like your goal is to rotate through an array of pointers...

what are you trying to do with that?

BulldogLowell:
sounds like your goal is to rotate through an array of pointers...

Yes, the solution that screams out is an array of pointers. The fact that you don't want to do this is puzzling.

You could use a std::vector to keep track of your arrays, but in the end you should avoid using dynamic memory too much since the arduino is limited in RAM and fragmentation may become an issue. If you sketch is supposed to be stable during the long haul, you should reserve all used memory in setup().

You can declare both your pointers and put them in an array, like below

you can handle them as list and individual

//    FILE: demo.ino
//  AUTHOR: Rob Tillaart 
// VERSION: 0.0.1

char firstAr[10] = "First";
char secondAr[10] = "Second";
char thirdAr[10] = "Third";

char* arPtrOne = firstAr;
char* arPtrTwo = secondAr;
char* arPtrThree = thirdAr;

char* myList[10] = { arPtrOne , arPtrTwo, arPtrThree };

void setup() 
{
  Serial.begin(115200);
  Serial.println(__FILE__);

  for(int i = 2; i >= 0; i--)
  {
    Serial.println(myList[i]);
  }
  for(int i = 0; i < 3; i++)
  {
    Serial.println(myList[i]);
  }
}

void loop()
{
  
}

gfvalvo:
Yes, the solution that screams out is an array of pointers. The fact that you don't want to do this is puzzling.

I would love to do it that way but im working on code that is already written and has hundreds of dependancies, in various way on the existing array pointers, so I need to use them. Otherwise if I change my pointers, there types, how they are declared, stored, initialized etc I will have to change my code in so many other places to match, which I'm trying to avoid. So I need to just point the exsisting pointers at new char arrays. As I said above I can do this in statements but I really didnt want 1000 or 2000 statements in the code if there was a way I could use a for loop to do it. Hopefully that helps you understand why im being so specific

robtillaart:
You can declare both your pointers and put them in an array, like below

you can handle them as list and individual

//    FILE: demo.ino

//  AUTHOR: Rob Tillaart
// VERSION: 0.0.1

char firstAr[10] = "First";
char secondAr[10] = "Second";
char thirdAr[10] = "Third";

char* arPtrOne = firstAr;
char* arPtrTwo = secondAr;
char* arPtrThree = thirdAr;

char* myList[10] = { arPtrOne , arPtrTwo, arPtrThree };

void setup()
{
  Serial.begin(115200);
  Serial.println(FILE);

for(int i = 2; i >= 0; i--)
  {
    Serial.println(myList[i]);
  }
  for(int i = 0; i < 3; i++)
  {
    Serial.println(myList[i]);
  }
}

void loop()
{
 
}

Hi Rob, this is exactly how I was trying to attack it earlier today. I was then planning on having a for loop that would go from 0 to 2 and for each loop around it would point the character pointer in myList at that index to a different char array. The part I was stumped on was how to write the statement that actually uses the myList arrays to do this. I tried everyway I could but couldnt get it to work correctly. Any thoughts? If I could just figure out how to write this statement correctly I would be completely sorted.

Do you mean this ?

//    FILE: demo2.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.0.1
// http://forum.arduino.cc/index.php?action=post;topic=529860.0

char firstAr[10] = "First";
char secondAr[10] = "Second";
char thirdAr[10] = "Third";

char* myList[10] = { firstAr , secondAr, thirdAr };

void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);

  for (int i = 2; i >= 0; i--)
  {
    Serial.println(myList[i]);
  }
  Serial.println();
  
  for (int i = 0; i < 3; i++)
  {
    Serial.println(myList[i]);
  }
  Serial.println();

  // swap two elements
  char * tmp = myList[0];
  myList[0] = myList[1];
  myList[1] = tmp;

  for (int i = 0; i < 3; i++)
  {
    Serial.println(myList[i]);
  }
  Serial.println();
}

void loop()
{
}

I’m a newbie trying to get his head around this; am I reading it correctly that you need to keep the pointer names (arPtrOne, arPtrTwo...arPtrninetynine) the same, but change the array they point at?

I get confused by multidimensional arrays, so I’d just set up three individual ones;

pointernames[100]
Oldtarget[100]
Newtarget[100]

Then

for (p=0;p<=100;p++){
 char pointernames[p]= newtarget[p];
}

oldtarget[] would be there if you needed to reverse the process

I’ve just looked up pointers and it seems way above my level so if the idea above is total gibberish, please ignore me!

for

GreyArea:
I’m a newbie trying to get his head around this; am I reading it correctly that you need to keep the pointer names (arPtrOne, arPtrTwo...arPtrninetynine) the same, but change the array they point at?

I get confused by multidimensional arrays, so I’d just set up three individual ones;

pointernames[100]
Oldtarget[100]
Newtarget[100]

Then

for (p=0;p<=100;p++){

char pointernames[p]= newtarget[p];
}




oldtarget[] would be there if you needed to reverse the process

I’ve just looked up pointers and it seems way above my level so if the idea above is total gibberish, please ignore me!

The idea of a pointer is not so difficult.

A pointer is a variable that holds the ADDRESS of another variable.

Imagine a matchbox in which you put a note with the address of a friend

you can copy that note and put it in another matchbox or
you can put anothers friends address in the matchbox.

two dimensional array's are like a chess board and every square can hold a value
three dimensional arrays are like a cube of sugar cubes, every sugarcube holding a value
four dimensional arrays are like a long table with multiple cubes of sugarcubes
five dimensional arrays are like a big chess board with every square a cube of sugarcubes
six dimensional arrays are like a BIG cube of smaller cubes.

seven dimensional arrays are like you need a database and redo your design :wink:
(maybe even earlier)

robtillaart:
seven dimensional arrays are like you need a database and redo your design :wink:
(maybe even earlier)

Your whole post was just for that joke, wasn’t it?

I like you. :slight_smile:

GreyArea:
Your whole post was just for that joke, wasn’t it?

I like you. :slight_smile:

No it started far more serious, but in the end ....

robtillaart:
No it started far more serious, but in the end ....

I understand arrays. They’re just boxes. Boxes that contain other boxes. Which may also contain boxes.

But in terms of code...

Array[3] = {1,2,3}

Is easy, that’s just a list

Array [3][3] = {
{1,2,3},
{4,5,6},
{7,8,9},
}

Is also easy, it’s just a spreadsheet. Columns and rows.

But

Array [3][3][3] =

Nope. Lost. I can never figure out a way to lay it out so I can easily read it. I know it’s just 3D coordinates...your sugar cube example, three layers of 3x3...but I can’t get my head around how to display it...

Array [3][3][3][3] =

No. Just no.

OK, last try to explain.

What is the address of your socks? It is also a multidimensional array, the indexes are

country city street number room cabinet drawer .... there is the sock

so if you want to make an Array [3][3][3][3] =

give good descriptive name for every index. like you should for every varaiable.

A three dimensional example with descriptive indices

averageTemperature [dayOfYear][lattitude][longitude]

A 4 dim example could be the raw values of every hour

rawTemperature [dayOfYear][lattitude][longitude][hour]

robtillaart:
Do you mean this ?

//    FILE: demo2.ino

//  AUTHOR: Rob Tillaart
// VERSION: 0.0.1
// Efficient way to re-assign char pointers to char arrays - Programming Questions - Arduino Forum

char firstAr[10] = "First";
char secondAr[10] = "Second";
char thirdAr[10] = "Third";

char* myList[10] = { firstAr , secondAr, thirdAr };

void setup()
{
 Serial.begin(115200);
 Serial.println(FILE);

for (int i = 2; i >= 0; i--)
 {
   Serial.println(myList[i]);
 }
 Serial.println();
 
 for (int i = 0; i < 3; i++)
 {
   Serial.println(myList[i]);
 }
 Serial.println();

// swap two elements
 char * tmp = myList[0];
 myList[0] = myList[1];
 myList[1] = tmp;

for (int i = 0; i < 3; i++)
 {
   Serial.println(myList[i]);
 }
 Serial.println();
}

void loop()
{
}

Hi Rob,

No that's not exactly it but closer than anything that has been suggested so far, your loop would swap the contents of the array that we have created but what I was trying to do was redirect the pointers I already have to point at new text arrays. Thank you so much though, I have solved my issue and it is all thanks to the replies I have received on here. Although none of them solved the issue directly, the ideas I have generated to solve my issues have come from suggestion and discussions had on here, so again thank you.

My solution: As I could not figure out how to point the existing pointers at the new char inside a loop I decided to fill the original arrays with the new char arrays contents. To do this I created all of the new char arrays inside an array char pointers so that I could reference them in a for loop. I then created a new array of char * and also pointed them to the original arrays. I then used a strcpy inside a for loop to copy the contents of the new array to the original. This means that the original pointers remain unaltered and still point to the original arrays but now the arrays have updated contents. This has the same effect as pointing the pointers at new c arrays without having to actually do so.

char firstAr[10] = "First";
char secondAr[10] = "Second";
char thirdAr[10] = "Third";

char* arPtrOne = firstAr;
char* arPtrTwo = secondAr;
char* arPtrThree = thirdAr;


char *newStrings[3][10]=
{
    "Fourth",
    "Fifth",
    "Sixth"
};

char *newPtrs[3]=
{
    firstAr,
    secondAr,
    thirdAr
};


for(int i = 0; i<3;i++)
{
    strcpy (newPtrs[i], newStrings[i]); 
}