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,
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.
@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?
@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...
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().
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
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.
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!
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
(maybe even earlier)
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...
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.