I am trying to copy an pointer to an array of arrays so I can pass it to other functions (as I understand it you can't pass an array of arrays directly to a function, but you can "flatten" it to one dimension first.)
The original data structure is a XPM bitmap, it looks like this:
/* XPM */
static char * numbers72x46_0_xpm[] = {
"46 72 2 1",
" c #000000",
". c #FFFFFF",
"..............................................",
"..............................................",
// 70 more lines like that
};
Just for starters, I am trying to make sense of how the array looks I'm memory by doing this:
void setup () {
char * xpm;
char b = 'X';
int i;
Serial.begin(19200);
Serial.println(numbers72x46_0_xpm[0]);
Serial.println(numbers72x46_0_xpm[1]);
Serial.println(numbers72x46_0_xpm[2]);
Serial.println(numbers72x46_0_xpm[3]);
Serial.println(numbers72x46_0_xpm[4]);
Serial.println();
// As I understand it this assignment should copy the pointer
// to numbers72x46_0_xpm to xpm, yielding a pointer to a one
// dimensional array
xpm = * numbers72x46_0_xpm;
// Why is xpm not a pointer to a one dimensional buffer with all
// of numbers72x46_0_xpm as a 1-dimensional array?
for (int i=0; i<200; i++) {
if (xpm[i] == '\0') {
Serial.println("<NUL>");
} else {
Serial.print(xpm[i]);
}
}
}
So, the first five line that have static array indexes print fine, then, in the loop, the first four lines are what I expect, but after that, garbage.
46 72 2 1
c #000000
. c #FFFFFF
..............................................
..............................................
46 72 2 1<NUL>
c #000000<NUL>
. c #FFFFFF<NUL>
..............................................<NUL>
<NUL><NUL>
^<NUL>
<NUL>
<NUL>
...
I have not allocated any memory to rpm because, as I understand it, I am copying a pointer to what I think is all of numbers72x46_0_xpm to "xpm" so I don't need to. Is that completely wrong? I seems to be only a pointer to numbers72x46_0_xpm[0].
If I try to
xpm = numbers72x46_0_xpm;
I get an error
cannot convert 'char* [75]' to 'char*' in assignment
.
Is there no way to do this except to allocate a buffer for "xpm" and them copy the XPM data to it?
I've searched all over and found plenty of examples for passing 1-dimensional arrays by reference, but not two-dimensional ones.
Thanks
w