I don't have clear idea how the following two pieces of code show different behavior:
code:
#include <stdio.h>
void set(char** addr) {
char* str = "testa";
*addr = str;
}
void _set(char*** addr) {
char* arr[] = {"testb"};
*addr = arr;
}
int main() {
char* a;
set(&a);
printf("'%s'\n", a);
printf("'%s'\n", a);
char** b;
_set(&b);
printf("'%s'\n",b[0]);
printf("'%s'\n",b[0]);
}
Output:
testa
testa
testb
testb
When I remove the first bit, the testa part, the code is:
void _set(char*** addr) {
char* arr[] = {"testb"};
*addr = arr;
}
int main() {
char** b;
_set(&b);
printf("'%s'\n",b[0]);
printf("'%s'\n",b[0]);
}
Output:
'testb'
'UH▒▒AWE1▒AVAUATSH▒▒8▒E▒'
The
seta()
function definesstr
as a pointer to a string. The string itself is an array of characters't','e','s','t','a','\0'
located on the heap.The
_seta()
function defines something completely different: an array of (one) pointers to (one) strings. The array itself is located on the stack, meaning that the array will go out of scope (i.e. get thrashed) as soon as the function returns. The string itself is another array of characters't','e','s','t','b','\0'
, which is located on the heap, just as above.Thus: the call
_set(&b);
obtains a pointer to undefined memory. The fact that everything seemed to work when you calledset(&a);
beforehand, was just pure bad luck.