By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

why pass-by-reference of a pointer variable?

P: n/a
Dear All,

I thought I understood using pointer variables as function parameters.
But I failed to understand why it is needed pass-by-reference of a
pointer variable.

To me, pointer variable is address variable, which holds the memory
address of the object. Using a pointer variable as a function parameter,
the function has the ability to CHANGE the object value pointed by the
pointer. Why needs pass by reference?

For example, I saw some code like the following:

void myfunctionA(int* pA);

void myfunctionB(int*& pA); //what is the advantage?

Thank you very much.

Dec 30 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Xiaoshen Li" <xl**@gmu.edu> wrote in message
news:dp***********@osf1.gmu.edu...
Dear All,

I thought I understood using pointer variables as function parameters. But
I failed to understand why it is needed pass-by-reference of a pointer
variable.
Most likely because the function will modify its value.

To me, pointer variable is address variable, which holds the memory
address of the object.
It can represent the address of an object.
Using a pointer variable as a function parameter, the function has the
ability to CHANGE the object value pointed by the pointer.
By dereferencing the pointer, yes it does. Another way would be
to use a reference instead of a pointer.
Why needs pass by reference?
This depends upon what the function does.

For example, I saw some code like the following:

void myfunctionA(int* pA);
'myfunction' can modify (and/or inspect) what 'pA' points to.
It cannot modify the caller's argument represented by 'pA'.

void myfunctionA(int *pA)
{
*pA = 0; // OK
pA = 0; // 'pA' is destroyed when function returns,
// caller's original argument is unchanged
}

void myfunctionB(int*& pA); //what is the advantage?


Again, 'myfunction' can modify (and/or inspect) what 'pA' points to.
But now, since it has a reference to the caller's argument (the pointer),
it can also modify that caller's argument.

void myfunctionB(int*& pA)
{
*pA = 0; // OK
pA = 0; // modifies caller's argument
}

int main()
{
int i = 1;
int *p = &i;
std::cout << i << '\n'; // prints 1
std::cout << p << '\n'; // prints address of 'i'

myfunctionA(p);
std::cout << i << '\n'; // prints 0

myfunctionB(p);
std::cout << p << '\n'; // prints NULL pointer value'
}

-Mike
Dec 30 '05 #2

P: n/a
Xiaoshen Li wrote:
Dear All,

I thought I understood using pointer variables as function parameters.
But I failed to understand why it is needed pass-by-reference of a
pointer variable.

To me, pointer variable is address variable, which holds the memory
address of the object. Using a pointer variable as a function parameter,
the function has the ability to CHANGE the object value pointed by the
pointer. Why needs pass by reference?

For example, I saw some code like the following:

void myfunctionA(int* pA) {
static int a[] = { 1, 2 };
pA = a;
}
void myfunctionB(int*& pA) //what is the advantage?

{
static int a[] = { 1, 2 };
pA = a;
}

One does what I expect, the other does not.

It does what this dies:

void myfunctionB(int** pA)
{
static int a[] = { 1, 2 };
*pA = a;
}
Dec 30 '05 #3

P: n/a
Xiaoshen Li wrote:
Dear All,

I thought I understood using pointer variables as function parameters.
But I failed to understand why it is needed pass-by-reference of a
pointer variable.

To me, pointer variable is address variable, which holds the memory
address of the object. Using a pointer variable as a function parameter,
the function has the ability to CHANGE the object value pointed by the
pointer. Why needs pass by reference?

For example, I saw some code like the following:

void myfunctionA(int* pA);

void myfunctionB(int*& pA); //what is the advantage?
You can then modify the pointer itself, apart from the
pointee.
Thank you very much.


you're welcome,
- J.
Dec 30 '05 #4

P: n/a
check this out:

template<typename T>
void better_delete(T*& ptr) {
delete ptr;
ptr = 0;
}

you delete, and set the pointer to null. this way it's, say, safe to call
better_delete twice on the same pointer :)

"Xiaoshen Li" <xl**@gmu.edu> wrote in message
news:dp***********@osf1.gmu.edu...
Dear All,

I thought I understood using pointer variables as function parameters.
But I failed to understand why it is needed pass-by-reference of a
pointer variable.

To me, pointer variable is address variable, which holds the memory
address of the object. Using a pointer variable as a function parameter,
the function has the ability to CHANGE the object value pointed by the
pointer. Why needs pass by reference?

For example, I saw some code like the following:

void myfunctionA(int* pA);

void myfunctionB(int*& pA); //what is the advantage?

Thank you very much.

Dec 30 '05 #5

P: n/a
Thank you all. Now I think I understand much better now. I still have
one more question:

For a function taking a pointer variable as parameter, actually I saw
two ways to pass the parameter:
void myfunctionA(int* pA)
{
*pA += 1;
}

int main()
{
int *pInt = new int;
*pInt = 77;
myfunctionA(pInt);
cout << *pInt << endl; //print out 78

//another way of passing parameter

int iNum = 99;
myfunctionA(&iNum);
cout << iNum << endl; //print out 100, correct?

return 0;
}

My question is: for myfunctionB()

void myfunctionB(int*& pA)
{
*pA += 2;
}

Is the following line ok?
myfunctionB(&iNum);

I feel lost with so many &.

Thank you very much.

Dec 30 '05 #6

P: n/a

"Xiaoshen Li" <xl**@gmu.edu> wrote in message
news:dp***********@osf1.gmu.edu...
Thank you all. Now I think I understand much better now. I still have one
more question:

For a function taking a pointer variable as parameter, actually I saw two
ways to pass the parameter:
void myfunctionA(int* pA)
{
*pA += 1;
}

int main()
{
int *pInt = new int;
*pInt = 77;
myfunctionA(pInt);
cout << *pInt << endl; //print out 78

//another way of passing parameter

int iNum = 99;
myfunctionA(&iNum);
cout << iNum << endl; //print out 100, correct?
Correct.

return 0;
}

My question is: for myfunctionB()

void myfunctionB(int*& pA)
{
*pA += 2;
}

Is the following line ok?
myfunctionB(&iNum);
No. Because a temporary object (the pointer returned by the
& operator and passed as the argument) cannot be bound to a
non-const reference.

Write it like this and it will be OK:

void myfunctionB(int* const& pA) /* 'pA' is ref to const pointer to
non-const int */
{
*pA += 2;
}

I feel lost with so many &.


You just need to realize that '&' is used for different things
depending upon context (expressed with different syntax).

void f(int& arg); // '&' denotes a reference

int i = 42;
int *p = &i; // '&' denotes 'address of'

int j = i & 2; // '&' denotes bitwise 'and' operation

j = i && 1 // '&&' denotes logical 'and' operation

-Mike
Dec 30 '05 #7

P: n/a
Xiaoshen Li wrote:

For example, I saw some code like the following:

void myfunctionA(int* pA);

void myfunctionB(int*& pA); //what is the advantage?


I hope this will be helpful:
http://www.codeproject.com/cpp/PtrToPtr.asp

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 30 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.