Why can I create an array of pointers and dereference its (pointer-)elements.
int a = 1;
int* arr[1];
arr[0] = &a;
But cannot do the same with pointer to pointers:
int** arr2;
arr2[0] = &a;
--> Seg fault
int** arr2;
You don't initialize this pointer. Accessing it is undefined behavior and can/will result in crashes.
To initialize use something like int** arr2 = malloc(<someSize> * sizeof(*arr2))
. (PS: malloc
may return NULL
, and you have to use free
to return the memory).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments