Skip to main content
GameDev.net gamedev.net
🔒 Locked

passing pointers

Started by GekkoCube Sep 5, 2003 at 7:37 PM 14 replies 1.5k views
Original Post
GekkoCube
GekkoCube
im wondering why passing a pointer to a function, and then setting that pointer to something in the function, does not return it back. example:
void SetValue(char *name)
{
  name = "billy joel";
}

void SetValueTwo(char *&name)
{
  name = "billy gates";
}

void main()
{
  char *name = "billy madison";
  SetValue(name);
  printf("%s\n",name);
  // the above will print "<null>"


  SetValueTwo(name);
  printf("%s\n",name);
  // the one above will print "billy gates"

}

i cant understand the passing by reference and why that works.
but im not sure why passing as a simple pointer would NOT work, since I am passing a pointer to a string.

Sneftel
Sneftel
Because you''re passing the pointer by value, not by reference. All variables passed by value, even pointers, are copied, so changes made to the copy don''t affect the original.

The reason that you can use pointers to affect whatever the pointer points to is that although the pointer itself is copied, the thing that the pointer points to is not. So both pointers point to the same place, so if you change whatever the one pointer points at, the other pointer will point at that same changed data.

But here, you''re not modifying the memory that is pointed to by the pointer, you''re modifying where the pointer itself points. And that pointer is the copy.


How appropriate. You fight like a cow.
evillive2
evillive2
quote:
void SetValue(char *name)
{
name = "billy joel";
}


you can't do that with C style strings. instead of
name = "billy joel";  

try
strcpy( name, "billy joel" );  

or use C++ strings.

edit:
well, what sneftel said is much better so feel free to ignore all of that

[edited by - evillive2 on September 5, 2003 8:46:49 PM]
Evillive2
GekkoCube
GekkoCube
no matter how many times i read what you said neftel i just dont get it!

i understand the passing by reference completely.

but if i pass a pointer by value, a copy of it is made. so far so good.

and if i change what the pointer is pointing to...in this case char *name, then it will change in the function''s pointer, but outside of the pointer stays the same because it has it''s own copy of what its pointing too?

Huh?!
Miserable
Miserable
When you change the pointer in the function, you change its value. This value is an address (to the location the pointer is pointing to). Now, as you well know, if you pass a variable by value and change the local one, the one passed remains unchanged. If you want to make the pointer point to a different location, you must pass it by reference (or by pointer-to-pointer).
GekkoCube
GekkoCube
neat, this is becoming a little more clear about all this.

i have one more thing on my mind.

lets say i have a global vector.
and from inside of a function i make a node and store it in the vector.

then i print the contents of the vector outside of this function.
i would think that nothing will be in there since every instance of Node that i created in the function will disappear (since they are all local to that function).

example:

//global

vector<Node> list;

void SetData()
{
Node node(5);
list.push_back(node);
}

void main()
{
printf("%d\n",list[0]);
}


if a node* was placed in the vector list then i know the memory wont go away (since i have to explicity delete it).
but since i did not create with a pointer, it must disappear since it is deleted upon exiting from the function.
GekkoCube
GekkoCube
oh, i forgot the question.

having said my post above, i can say for sure, due to my testing this, that i can still print out the proper values.

now, this i dont understand because of my reasons stated above.

can someone explain this one?
Sneftel
Sneftel
The member function push_back of a vector internally makes a copy of whatever you pass into it, into memory it allocates itself using new. So yes, the value you passed in gets deleted upon exiting the function, but at that point it''s already been copied into the vector''s memory space.


How appropriate. You fight like a cow.
Malone1234
Malone1234
Yes, vector handles all memory management internally so you don''t have to worry about it! As such, it would be nice if the data in the vector didn''t disappear from funtion to function. So, to do that, every element in a vector is actually stored on the heap, though that''s supposed to be transparent to us.
GekkoCube
GekkoCube
what if i pass in variables to a function, and inside the function i create a Node, like this....

Node node;

then i give it some values and insert it into a regular array (which is defined outside of the function)

when i exit the function, the data will be gone, correct?
since an array doesnt make a copy of it.
brassfish89
brassfish89
quote:

what if i pass in variables to a function, and inside the function i create a Node, like this....

Node node;

then i give it some values and insert it into a regular array (which is defined outside of the function)

when i exit the function, the data will be gone, correct?
since an array doesnt make a copy of it.


you can''t assign a class to a class. ie, this would not be legal:

class Class
{
// blah
};
Class c1;
Class c2;
// c1=c2 is illegal

however, you can do that if you implement the = operator to make a copy:

class Class
{
int i1,i2;

public:

// constructor
Class(int _i1,int _i2):i1(_i1),i2(_i2){}

// equals operator
Class& operator=(Class& c)
{
// copy all of c''s data
i1=c.i1;
i2=c.i2;
return *this;
}
};

Class c1(1,2);
Class c2(5,6);

c1=c2; // legal

Also, incase you didn''t know, assigning something to an array element is exactly the same as assigning a normal variable to another. Also, to answer your question, if you used the code above, then the data will not be gone, since it was coppied into the array element.


andromeda
andromeda
quote:
Original post by Sneftel
Because you''re passing the pointer by value, not by reference. All variables passed by value, even pointers, are copied, so changes made to the copy don''t affect the original.

The reason that you can use pointers to affect whatever the pointer points to is that although the pointer itself is copied, the thing that the pointer points to is not. So both pointers point to the same place, so if you change whatever the one pointer points at, the other pointer will point at that same changed data.

But here, you''re not modifying the memory that is pointed to by the pointer, you''re modifying where the pointer itself points. And that pointer is the copy.


How appropriate. You fight like a cow.
So, just to test my memory of pointers: If he dereferenced the pointer in the function, then the program would work with the desired effect, right?
Sneftel
Sneftel
quote:
Original post by andromeda
So, just to test my memory of pointers: If he dereferenced the pointer in the function, then the program would work with the desired effect, right?

Kind of, tho he''d still have to use strcpy to actually copy all the characters over. But this isn''t legal, since the pointer points to a string literal, which is supposed to be const. The correct method would be for him to pass in a double pointer, and dereference that.


How appropriate. You fight like a cow.
GekkoCube
GekkoCube
i was thinking about storing the address of a struct in an array (or any single variable).


struct Node
{
int data;
};

Node* list[3];

void Set()
{
Node node;
node.data = 7;
list[0] = &node
}

void Show()
{
printf("%d\n", list[0]->data);
}



Now this works....BUT i stored the address of node into array[0]. and i never overloaded the = operator either.
and all that was done localy in a function and then printed elsewhere. now shouldn''t the contents of node in list[0] disappear?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.