# memory address of a pointer passed by value

This topic is 3463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

#include <iostream>

using namespace std;

struct superfun
{
int inumber;
};

void blitz(superfun **value, superfun **worthless);

int main()
{
superfun *inum;
superfun *tricky;

inum=new superfun;
tricky=new superfun;

inum->inumber=5;
tricky->inumber=21;
blitz(&inum,&tricky);

cout<<"inum->inumber="<<inum->inumber<<"\n";

delete inum;
delete tricky;

return 0;
}

void blitz(superfun **value, superfun **worthless)
{
superfun *temp;

temp=*worthless;
*worthless=*value;
*value=temp;
}


Alright I think I got an interesting problem for you guys and probably it's a fast answer. I have a problem with my program so I wrote the above code on the side to try to figure out a solution. The above code works as is. Look at the blitz function. I had something like that and originally I something like
void blitz(superfun *value, superfun *worthless)
{
superfun *temp;

value->inumber+=2;
temp=worthless;
worthless=value;
value=temp;
}


My original intention was to add 2 to inum->inumber and then to make tricky and inum trade what they were pointing at. However it turned out the last three lines in the blitz function above were worthless because those were passed by VALUE. value->inumber+=2 added 2 to inum->inumber back in the main function because value and inum both had a copy of the same memory address. But value was only a COPY of inum so changing what it pointed at in blitz didn't effect what inum pointed at. value went out of scope and my output was 7. So then I wrote the code at the very top and I figured out how to swap inum and itricky. My output is now 21. I pass the ADDRESS of a pointer. Then by doing this:
temp=*worthless;
*worthless=*value;
*value=temp;


value and worthless are of type **superfun so when I modify *value then I'm treating the value of **value as a memory address and I'm changing the value that is stored at that memory address which is the value of inum back in my main funcion. But now I don't know how to do what I originally could do which was add 2 to inum->inumber. What is the syntax to do that? I have value and it's the memory address of the memory address that points to a superfun structure which contains inumber. But I don't know how to get at inumber anymore.

##### Share on other sites
(*value)->inumber += 2

##### Share on other sites
Sweet, that did the trick. By the way if there's a better way to do this anyone can feel free to tell me. It seems to me the best way to both add 2 to inum->inumber and swap what inum and itricky point to in a function is to pass them as **value. I don't see how else to do it.

##### Share on other sites
void blitz(superfun*& value, superfun*& worthless)
{
superfun *temp;

value->inumber += 2;

temp=worthless;
worthless=value;
value=temp;
}

think that works the same

##### Share on other sites
[rolleyes]
void blitz(superfun *&value, superfun *&worthless){  superfun->value += 2;  std::swap(value, worthless);}