I wanted to change the value of a local pointer variable in C. The program
below should illustrate:
#include <stdio.h>
void func(int *p)
{
*p++;
printf("%X\n", p);
}
int main()
{
int *ptr = malloc(2*sizeof(int));
printf("%X\n", ptr);
func(ptr);
printf("%X\n", ptr);
}
The pointer value does not change after the function call. I figured out the
solution so here it is:
#include <stdio.h>
void func(int **p)
{
printf("%X\n", *p);
(*p)++;
printf("%X\n", *p);
}
int main()
{
int *ptr = malloc(2*sizeof(int));
printf("%X\n", ptr);
func(&ptr);
printf("%X\n", ptr);
}
The idea is to pass a pointer to the pointer!
Regards
Manohar Vanga