Memory allocated with malloc does not persist outside function scope?
- by PM
Hi,
I'm a bit new to C's malloc function, but from what I know it should store the value in the heap, so you can reference it with a pointer from outside the original scope. I created a test program that is supposed to do this but I keep getting the value 0, after running the program. What am I doing wrong?
int f1(int * b) {
b = malloc(sizeof(int));
*b = 5;
}
int main() {
int * a;
f1(a);
printf("%d\n", a);
return 0;
}