I am an C beginner and I do not understand why I am getting an error here. I was taught that a pointer survives a function with malloc
, but here I get a code dumped
.
I need to get this and can't find anything on the Internet. I know that you can do it completely different, but I didn't want to use global variables or a pointer to pointer. I'd love to learn about malloc
!
#include <stdio.h>
#include <stdlib.h>
void doStuff();
void doStuff(int *a) {
a = malloc(sizeof(int));
*a = 3;
printf("%d \n", *a);
};
int main() {
int *a;
doStuff(a);
printf("%d", *a);
free(a);
}