passing pointers not by value
Is there any way to do this?
I have a function that sets a pointer to NULL. But after the funtion ends, the pointer isnt NULL. Any way to do this?
Thanks.
You can pass the pointer by reference, or a pointer to the pointer. e.g.:
void some_function(void * & ptr) { ptr = 0;}
Of course you can also, use a pointer to a pointer ;)
void some_function(datatype ** ptr) { *ptr = NULL;}
I am not sure, but is this what you are talking about:
And you use it like this:
void MyFunc(int * ptr){if(ptr != 0){delete ptr;ptr = 0;}}
And you use it like this:
int * ptr;ptr = new int;MyFunc(ptr);
Quote:Original post by Amnesty
Is there any way to do this?
I have a function that sets a pointer to NULL. But after the funtion ends, the pointer isnt NULL. Any way to do this?
Thanks.
Is the function's only purpose to set the pointer to NULL? If so, you'd be saving time by just setting it to NULL instead of calling a function to do it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement