-
December 7th, 2010, 11:39 AM
#1
Question about using pointer with reference
Hi everyone!
I have problems in using pointer with reference. So, I make simple program to clarify them.
First of all, this is my code and its output. Please notice line (*) and (**):
My code:
//////////////////////////////////////////////////////////////////////////////////////////
void RefFunc(int*& a){
int q = 888;
a = &q; //PROPLEM HERE (*) - //*a = q
cout << "address of a after assign= " << &a << endl;
cout << "value of a after assign= " << *a << endl;
}
int main ()
{
int * ptr;
int p = 777;
ptr = &p;
RefFunc((int*&)ptr); //PROPLEM HERE(**) - ptr
cout << "address of ptr after RefFunc function = " << &ptr << endl;
cout << "value of ptr after RefFunc function = " << *ptr << endl;
getch();
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////
Output:
address of a after assign = 0012FF28
value of a after assign = 888
address of ptr after RefFunc function = 0012FF28
value of ptr after RefFunc function = 1730693832
//////////////////////////////////////////////////////////////////////////////////////////
These are what I tried:
1- I think "value of ptr after RefFunc function" should be the same with the value of variable 'a' in RefFunc because I use pointer - reference (*&) in RefFunc function. But it is not...(see the output)
2- When I modified line (*) to "*a = q": this time, "value of ptr after RefFunc function" is the same with 'a' in RefFunc (888).
3- In line (**) - I used RefFunc((int*&)ptr) or RefFunc(ptr): these two ways have the same results to output.
Anyone can explain for me the difference between:
1- <a = &q> and <*a = q> in line (*) in this situation, why *ptr does not change its value when I use <a = &q> whereas it does when I used <*a = q>?
2- <RefFunc((int*&)ptr)> and <RefFunc(ptr)>: what does it means in this case?
Thanks everyone in advance!!
-
December 7th, 2010, 01:10 PM
#2
Re: Question about using pointer with reference
Originally Posted by lordelf2004
Hi everyone!
I have problems in using pointer with reference.
How is this related to the Windows API? You should post in the non-Visual C++ forum, not this one.
Regards,
Paul McKenzie
-
December 7th, 2010, 01:18 PM
#3
Re: Question about using pointer with reference
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|