CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2008
    Posts
    13

    Smile 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!!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Question about using pointer with reference

    Quote Originally Posted by lordelf2004 View Post
    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

  3. #3
    Join Date
    Oct 2008
    Posts
    13

    Re: Question about using pointer with reference

    Thanks Paul McKenzie ,

    I have reposted to http://www.codeguru.com/forum/showth...25#post1985125

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured