Sign in to follow this  
icecubeflower

memory address of a pointer passed by value

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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this