Passing double pointer to function
-
Hi I am a bit confused about the double pointer. I would like to pass it to a function and change the pointer value:
static int GetValue(int** hello)
{
*hello = &Something;
}//Why does this work:
int* pointer = NULL;
GetValue(&pointer);//And this does not work:
int** pointer = NULL;
GetValue(pointer);To me they seem like the same thing.
-
Hi I am a bit confused about the double pointer. I would like to pass it to a function and change the pointer value:
static int GetValue(int** hello)
{
*hello = &Something;
}//Why does this work:
int* pointer = NULL;
GetValue(&pointer);//And this does not work:
int** pointer = NULL;
GetValue(pointer);To me they seem like the same thing.
elelont2 wrote:
To me they seem like the same thing.
No, they are totally different. Case 1: You create a pointer that will point to an integer variable, and pass its address to the function. The function then stores the address of a variable inside the pointer. On return the pointer now points to the variable, and the variable contains a value. Case 2: You create a pointer that will point to a pointer but is currently NULL. So when you pass it to the function the function gets a NULL value. Simply put, in case 1, hello points to the address of pointer, in case 2, hello contains NULL. If you step through the code with your debugger it should become clear.