I was about to use malloc or strdup to manage this.
Finally, I tried this snippet and it works on both my linux and in the more complex code I wrote based on this approach:
At line 12, you are not copying "hello modified" into the memory pointed to by list_copy[0]. You are changing the value of the pointer list_copy[0] to point to a different memory address, that's storing the string literal "hello modified".
However, the value of the pointer list[0] is still pointing to the memory it was originally pointing to - the string literal "hello".
A pointer is just a number. It's the number of an address in memory. When we say that a pointer "points" to something, we mean that it's value is set to to address in memory where that something is stored.
Consider if you were using arrays of ints, rather than pointers:
1 2 3 4 5 6 7 8 9 10
void test_function(int* list, int count)
{
int list_copy[count];
for(i = 0; i < count; ++i) {
list_copy[i] = list[i];
}
list_copy[0] = 12;
}
Would list_copy[0] = 12; also change the value of list[0]? Why?
If you can link me to some text book when initializing pointer arrays that explains this difference, it would be even better.
There's nothing special or magical about arrays of pointers. They work just the same as arrays of any other kind. The value stored in each element is a pointer, but that doesn't mean they behave any differently from any other array.
If you understand arrays, and you understand pointers, there's nothing new to understand about arrays of pointers.
Now that second version of the code is different from the first one you've posted, and behaves differently. Initialising list_copy the way you have done in line 5 of your second version is a different thing from what you did in lines 5 - 9 of your first version, and will result in different behaviour.
I wanted to understand what happens when I initialize in the second way
I guess since list_new is a pointer to pointer, all array members will directly point to the address of list[] ?
The name of any array functions as a pointer to the first element in the array (loosely speaking). So when you do:
char** list_copy = list;
You set the number (address) that's stored in list_copy to be the address of list[0].
So from then on, list_copy is pointing to exactly the same bit of memory as list points to, and everything follows on from there.
In the first version, list and list_copy were separate arrays, and so those names were pointers to different bits of memory. You were just setting the values of the elements to be the same in each array.