I have an interesting problem.
I define,
typedef char *string;
char array[10];
string buf[10];
i=0;
while(1){
array=<assign_string>
buf[i]= array;
i++;
}
At each iteration i assign different strings. For instance,
buf[0] should be “1111111111”
buf[1] should be “2222222222” and so on.
However when i assign “2222222222” when i=1, buf[0] also changes to “2222222222”. What could be the problem?
The problem is that all entries in
bufpoint to the same string – the one namedarray. So changingarraywill affect allbufentries.You could fix this by allocating a new string for every iteration, e.g. your pseudo-code would become:
Make sure to
free()all the strings when you’re done using them.