passing pointers not by value

Started by
4 comments, last by Rhaal 19 years, 5 months ago
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.
"I turned into a driveway I thought was mine and crashed into a tree I don't have."- a real insurance claim
Advertisement
You can pass the pointer by reference, or a pointer to the pointer. e.g.:
void some_function(void * & ptr) {  ptr = 0;}
Thank you.. Thought i tried that first.
"I turned into a driveway I thought was mine and crashed into a tree I don't have."- a real insurance claim
Of course you can also, use a pointer to a pointer ;)
void some_function(datatype ** ptr) {  *ptr = NULL;}
My site Jvitel.com
I am not sure, but is this what you are talking about:

void MyFunc(int * ptr){if(ptr != 0){delete ptr;ptr = 0;}}


And you use it like this:

int * ptr;ptr = new int;MyFunc(ptr);
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
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.
- A momentary maniac with casual delusions.

This topic is closed to new replies.

Advertisement