After a good night sleep I realized that with the statement: "myA[2] = myStr3;" I just copied pointers.
When this statement is executed "myStr3" AND "myA[2]" are pointing to the same memory location.
Then when the second statement "strcpy(myStr3, "&");" is executed, myStr3 is pointing to a memory location that contains the value "&".
And as "myStr3" AND "myA[2]" are pointing to the exact same memory location, I get "&" when I print the content of "myA[2]".
But,
knowing what's going on is half of the solution.
Next,
is finding out how to get "myA[2]" pointing to the location where the char string "myStr4" is pointing to.
Anytime you use an array name without the bracketed subscript (e.g., myStr3[0]) as you've done above, the expression on the right resolves to the memory address where myStr3 is stored. For most Arduinos, that's going to be a 16-bit piece of data. The assignment operator is going to attempt to take those 16 bits and shove them into an 8-bit address space, which will spill half of myStr3's memory address on the floor. As a general rule, with strings formed with char arrays need to be manipulated using one of the str*() or mem*() functions.
Anytime you use an array name without the bracketed subscript (e.g., myStr3[0]) as you've done above, the expression on the right resolves to the memory address where myStr3 is stored. For most Arduinos, that's going to be a 16-bit piece of data. The assignment operator is going to attempt to take those 16 bits and shove them into an 8-bit address space, which will spill half of myStr3's memory address on the floor. As a general rule, with strings formed with char arrays need to be manipulated using one of the str*() or mem*() functions.
No. myA is an array of pointers to char. myA[2] is a pointer to char. myStrs3 is also a pointer to char. The above line changes the pointer stored in myA[2] so it points to the char array stored at myStr3;
pagemaker:
Next,
is finding out how to get "myA[2]" pointing to the location where the char string "myStr4" is pointing to.
That would be:
myA[2] = myStr4;
instead of:
myA[2] = myStr3;
You are replacing the contents of myStr3 with a copy of the contents of myStr4. That gives you TWO arrays containing the same characters. But then you replace the contents of myStr3 with "&" so it no longer contained the same characters as myStr4. If you want to point myA[2] to the contents of myStr4 you should use myStr4.
Never, never, EVER use malloc() without checking the return value, or your program WILL crash if you've run out of memory. If malloc() returns NULL, you did NOT get any memory, and using the returned pointer WILL clobber RAM, starting at address 0.