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
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Question about using pointer with reference

    The (int*&) cast is unnecessary. In general, try to avoid casting to make parameter passing work---it can produce very subtle bugs if you're trying to pass something that really *isn't* the right type.

    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>?
    Two different statements. Remember, ptr is a variable; its value is the address of a different variable, in this case p in main().

    The statement a = &q modifies the value of a, which is a reference to ptr. ptr now has a value which is a reference to q, which is a local variable in the function. This is a bad idea, because that variable will no longer exist when the function returns.

    the statement *a = q modifies the value of the variable pointed to by a, which is p in main(), and assigns it the value of the variable q in the function. This is fine.

  3. #3
    Join Date
    Jan 2009
    Posts
    54

    Re: Question about using pointer with reference

    The key here is that RefFunc force p to point to the address of a local variable.
    The contents of stack values (like q) cannot be counted on after the function returns.

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