Consider two cases of calling a function:
void Convert(int number, int **staticPointer)
{
int * dynamicPointer = malloc(sizeof(int));
*dynamicPointer = number;
*staticPointer = dynamicPointer;
}
int main()
{
int *p;
Convert(5, &p);
printf("The number is: %d", *p);
free(p);
}
The above works beautifully with no problem.
Now the case below:
void Convert(int number, int *staticPointer)
{
int * dynamicPointer = malloc(sizeof(int));
*dynamicPointer = number;
staticPointer = dynamicPointer;
}
int main()
{
int *p;
Convert(5, p);
printf("The number is: %d", *p);
free(p);
}
This causes SEGFAULT. Notice that I do call the function in different ways with different parameters, and I do understand that the difference is in the calling of the function.
Can somebody please explain to me why I have to call the function as in the first example, and why the other example doesn't work (it crashes in main)?
The purpose of my program is to reference a static pointer to a specific memory address through a function.