I am trying to initialize a global pointer inside a function. This is an example that shows the same issue:
#include <stdlib.h>
#include <assert.h>
int* global = NULL;
void alloc(int* p) {
p = (int*)malloc(sizeof(int));
*p = 42;
}
int main() {
alloc(global);
assert(global);
}
After the call to alloc
global
is still NULL
. It works if I don't pass global
to the function but just use it since it's global.
I would rather keep the function call this way to make it more clear. Is there a way to achieve this? Thanks.
Btw. I'm using gcc 6.3.1