This question is tied to Making an Array to Hold Arrays of Character Arrays in C
Borrowing code from there, I have something that looks like this (credit to luser droog for the nice example code):
enum { BUFSZ = 50 };
enum { STRVSZ = 40 };
enum { STRVVSZ = 20 };
char buf[BUFSZ + 1];
char *strv[STRVSZ + 1];
char **strvv[STRVVSZ + 1];
int j;
int i;
while(1){
fgets(buf, BUFSZ, infile);
i = 0;
strv[i] = strdup(buf);
strv[i+1] = NULL;
j = 0;
strvv[j] = calloc(i+1, sizeof *strvv[j]); // assuming i is the count of elements
memcpy(strvv[j], strv, i * sizeof *strvv[j]);
j++;
}
This might not run right out the door, but it illustrates something similar to what I'm running. Basically, the contents of strv needs to be stored in strvv after every iteration of the loop, and strv changes over time based on user input.
Using calloc and memcpy should have caused strvv to maintain copies of strv at each iteration of the loop independent of the values in strv. However, when I print out the contents of strvv, it prints out the same string for every entry, implying that the current approach is still moving pointers around and not making copies of strv in each strvv entry.
I'm not at all sure why this happens or how to fix it. memcpy should be making a byte level copy of what the pointers in strv point to =/.
of
– luser droog Apr 19 '13 at 03:53s of strings. Where
is implemented as a dynamic array.