I have a function f1
which expects void pointer. From the caller I want to have generic logic of passing void pointer to A
which modifies the pointer internally.
sample code is pasted below :
#include "stdio.h"
#include "malloc.h"
void f1(void* a)
{
printf("f(a) address = %p \n",a);
a = (void*)(int*)malloc(sizeof(int));
printf("a address = %p \n",a);
*(int*)a = 3;
printf("data = %d\n",*(int*)a);
}
void f(void)
{
void* a1=NULL;
printf("a1 address = %p \n",a1);
f1(a1);
printf("a1 address = %p \n",a1);
printf("Data.a1 = %d\n",*(int*)a1);
}
int main()
{
f();
}
But it causes segmentation fault. Is there any way to make it work without changing prototype of function f1
?