I'm doing a little programming experiment in C on ways to represent multiple lines of text.
I've come up with four ways: arrays of arrays, arrays of pointers, pointers of arrays, and pointers of pointers.
I've run into a little problem with pointers of arrays. Every time I create an array, it actually just reuses the array I already created.
Check out the output of the "pointers of arrays" (pa) part of my program to see what I mean:
variable address value
========== ============== =====
pa 0x10f500970
*(pa+0) 0x7fff6f0334bd
*(pa+0)[0] 0x7fff6f0334bd t
*(pa+0)[1] 0x7fff6f0334be e
*(pa+0)[2] 0x7fff6f0334bf s
*(pa+0)[3] 0x7fff6f0334c0 t
*(pa+0)[4] 0x7fff6f0334c1
*(pa+0)[5] 0x7fff6f0334c2 0
*(pa+1) 0x7fff6f0334bd
*(pa+1)[0] 0x7fff6f0334bd t
*(pa+1)[1] 0x7fff6f0334be e
*(pa+1)[2] 0x7fff6f0334bf s
*(pa+1)[3] 0x7fff6f0334c0 t
*(pa+1)[4] 0x7fff6f0334c1
*(pa+1)[5] 0x7fff6f0334c2 1
See how the address for each array are the same? Not very cool, is it?
So here's my question: Is there a way to actually declare a new array in a n > 1 loop, rather than rewrite the contents of the already existing array?
Here's the snipplet: (LIST_SIZE = 2 and TEXT_SIZE = 6)
void** pa = malloc(LIST_SIZE * sizeof(void*));
printh();
printf("pa\t\t%p\n\n", pa);
for(int i = 0; i < LIST_SIZE; i++)
{
char txt[TEXT_SIZE + 1];
sprintf(txt, "test %d", i);
*(pa + i) = txt;
printf("*(pa+%d)\t\t%p\n\n", i, *(pa + i));
for(int j = 0; j < TEXT_SIZE; j++){
printf("*(pa+%d)[%d]\t%p\t%c\n", i, j, &(((char*)*(pa+i))[j]), ((char*)*(pa+i))[j]);
}
printf("\n");
}
Thanks. If you have any other comments or suggestions, I'd be interested in hearing them, too.
The full source code is available at: https://gist.github.com/80m/7143558