Just playing with pointers, trying to pass the address of a pointer to a function, using:
void changer(int **ptr)
{
if(**ptr==NULL)
{
*ptr=(int *)malloc(sizeof(int));
**ptr=2;
}
}
int main()
{
int *ptr;
clrscr();
changer(&ptr);
printf("%d",*ptr);
getch();
return 0;
}
Though the program compiles, the results are not as expected!
You have an extra dereference of
ptrin theNULLcheck, and you are not initializing the pointer. The program should look like this after the fix: