You should really ask this tagged as a C question -- this kind void* pointer tomfoolery is frowned on in C++. Although it is part of the language, and occasionally has its uses, you may have more luck getting the expertise you need from someone looking for C questions to answer.
Moving on to your question, I think what you want to do (at least the example you are posting) is simply impossible -- specifically:
void *myPtr = NULL;
foo(myPtr);
Consider what you are actually doing here. You're declaring a variable myPtr, with a void* type, and assigning it a placeholder value -- an invalid area of memory C programmers call NULL, which it typically 0 (C++ programmers prefer nullptr which is a specific type, providing some safety benefits).
Now you pass that to foo:
void foo(void *ptr) {
ptr = malloc(NUM * sizeof(TYPE));
}
foo receives a NULL pointer -- an invalid memory address which is typically 0. When you call the ptr = malloc(...)
statement your variable ptr
gets the value of the memory address returned by malloc -- which has allocated some memory for you and returned an address pointing to that memory.
You have not modified the contents of the variable myPtr, which contains an invalid memory adress NULL.
You need to change the signature of the function so that it can fill myPtr with a valid address -- the address of the memory you just allocated. You could do this with:
1: void* foo(void* ptr) { return malloc(...);} // return the address.
2: void foo(void** ptr) { *ptr = malloc(...);} // ptr to ptr.
3: void foo(void*& ptr) { ptr = malloc(...);} // C++: reference to ptr.
I don't see any other way around it. Incidentally I would prefer option 1, since at point of reading it's clearest what's happening:
myPtr = foo(myPtr);
And unless you're doing anything with the contents of myPtr in foo (e.g. you're checking if it's null and allocating if not), you should lose the parameter.