The variable txt
is local to the for
loop. At each iteration, the old txt
variable becomes invalid, and a different txt
instance is created. This means the pointer that was assigned txt
in the previous iteration is not pointing to a valid object when the next iteration starts.
When the loop is finished, none of your pointers point to anything valid.
One way to fix this is to dynamically allocate memory for each pointer you want to assign to.
pa[i] = malloc(TEXT_SIZE+1);
snprintf(pa[i], TEXT_SIZE+1, "test %d", i);
You say you want "pointers to arrays", yet you declared of void **pa
, which makes pa
a pointer to a pointer. If you want pa
to a pointer to an array, you would declare it this way:
char (*pa)[TEXT_SIZE+1] = malloc(LIST_SIZE * sizeof(*pa));
So now, pa[0]
would be one 7 byte array, and pa[1]
would be a second 7 byte array. You do away with the need to do any dynamic allocation within the loop.