simple question about passing pointer argument

please correct me if i am wrong, this is what i understood and duno if it's valid or not , please correct and complete what i've said :)

When you pass an address to a function, that address is actually passed by value! Because the address is passed by value, if you change the value of that address within the function, you are actually changing a temporary copy. Consequently, the original pointer address will not be changed!


you may change the value within the copy address you got (as you will be creating a new pointer to that copy address) but you can't change the address at this pointer as all what you did was sending the address pointed by the pointer (value of pointer) and not the pointer itself

and to change what this pointer points to you need to send an address for pointer of pointer, so you can change the actual address in the first level pointer by changing the value pointed by the second level pointer

so you send pointer1 pointing to pointer2 for an ex. int imint
by passing &pointer1 you can change the value of pointer2 which is the address of imint
and you still can use pointer2 value that you pass as an argument to change the value of imint

is that right ? any other way of getting it done ? thanks in advance and forgive my ignorance


edit: i've read more and figured out how to pass a reference of a pointer but i still want to make sure that the pointer of pointer idea was almost the same as of what i know that reference are built in within pointers logic so it should be the same , am i right ? sorry i still need to make sure of what i thought of :)
Last edited on
is that right ?

Yes, that's right. The thing you pass into a function is passed by value. So if you pass a pointer into a function, the value stored in the pointer - a memory address - is passed by value.

HOWEVER...

C++ has the concept of a reference. Instead of passing a pointer to an object, you can pass a reference to an object. Passing a reference into a function means that if you change the value of it within the function, that change is applied to the original object in the calling code.

Any C++ tutorial source should tell you about them. Wiki also has a page:

http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29
Last edited on
i already know that, i know it's boring to read all of my question with my poor english but your answer wasn't helpful to me :)
i was asking about how to change a pointer value (address of what it's pointing to)
i was asking if it's possible by passing pointer of pointer and then i found the way of passing a reference of the pointer but i still want to make sure that the way i thought of will be valid or not
wish this is as short as possible for you to read :)
Last edited on
Pass by reference does not pass a temp copy, it passes the actual object. So any changes made to this object in the function affects the actual object.

observe the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

int swap(int *A, int *B) {
	int C(*A);
	*A = *B;
	*B = C;
	cout << "Pointer swap called!\n";
	return 1;
}

int swap(int &A, int &B) {
	int C(A);
	A = B;
	B = C;
	cout << "Reference swap called!\n";
	return 1;
}

int main() {
	int r = 4, t = 5;
	cout << r << " " << t << endl;
	swap(r, t);
	cout << r << " " << t << endl;
	swap(*&r, *&t);
	cout << r << " " << t << endl;
	swap(&r, &t);
	cout << r << " " << t << endl;
    return 0;
}



4 5
Reference swap called!
5 4
Reference swap called!
4 5
Pointer swap called!
5 4


EDIT: Just saw your last post
>> was asking about how to change a pointer value (address of what it's pointing to)


The only way to change the address of what a pointer is pointing to is to simply point it at something else

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main() {
	int r = 4, t = 5, *p = &r;
	cout << r << " " << t << endl;
	cout << *p << endl;
	p = &t;
	cout << r << " " << t << endl;
	cout << *p << endl;
    return 0;
}


4 5
4
4 5
5


If this still does not answer your question, then maybe you can post sample code of what you are trying to do then we can help more?
Last edited on
You asked:

is that right ?

and I answered:

Yes, that's right.

If knowing whether or not it was right wasn't useful to you, then why waste your time asking it, and why waste my time making me think you wanted an answer to it?

i know it's boring to read all that but your answer wasn't helpful to me

Actually, it would have been quite exciting to suddenly gain the power to see into the future and read an edit that you hadn't made when I was writing my reply. It would have been by far the most interesting thing to happen to me all afternoon.

Sadly, I was unable to do so. I'm sorry if my lack of ability to read your mind made my response less than useful to you.

You'll be relieved to know that I won't waste your time by attempting to help you any further.
@MikeyBoy sorry for the misunderstanding :) your reply was mean so i guess we are even but mine was just a poor english and a misunderstanding more than of being a directly mean reply , sorry if what i said wasn't what you expecting :)

im sorry everyone ill try to work it out on my own as i can't explain the whole situation but ill try for one last time within code in a new clean thread
thanks everyone and sorry for the misunderstanding , ill give that another shoot now within a clean thread with code example
Topic archived. No new replies allowed.