How can I change a heap allocated global pointer from within a function? Here is what I am working with, (call to modify_p
seg faults and I can't see much in the debugger):
#include <stdio.h>
#include <stdlib.h>
int *p;
void set_p(int *p, int sz) {
p = malloc(sz * sizeof(int));
}
void modify_p(int *p) {
p[0] = 2;
}
int main(int argc, char *argv[])
{
set_p(p, 3);
modify_p(p);
//printf("%d\n", p[0]);
// should print 2
return 0;
}
Thanks