why pass-by-reference of a pointer variable?

Discussion in 'C++' started by Xiaoshen Li, Dec 30, 2005.

  1. Xiaoshen Li

    Xiaoshen Li Guest

    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.
    Xiaoshen Li, Dec 30, 2005
    #1
    1. Advertising

  2. Xiaoshen Li

    Xiaoshen Li Guest

    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.
    Xiaoshen Li, Dec 30, 2005
    #2
    1. Advertising

  3. Xiaoshen Li

    Mike Wahler Guest

    "Xiaoshen Li" <> wrote in message
    news:dp3lct$18mi$...
    > 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
    Mike Wahler, Dec 30, 2005
    #3
  4. 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;
    }
    Gianni Mariani, Dec 30, 2005
    #4
  5. 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.
    Jacek Dziedzic, Dec 30, 2005
    #5
  6. 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" <> wrote in message
    news:dp3lct$18mi$...
    > 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.
    >
    Martin Vorbrodt, Dec 30, 2005
    #6
  7. Xiaoshen Li

    Mike Wahler Guest

    "Xiaoshen Li" <> wrote in message
    news:dp3ujt$1dpj$...
    > 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
    Mike Wahler, Dec 30, 2005
    #7
  8. =?UTF-8?B?TWF0ZXVzeiDFgW9za290?=, Dec 30, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,737
    Smokey Grindel
    Dec 2, 2006
  2. Mr A
    Replies:
    111
    Views:
    2,055
  3. Robert
    Replies:
    10
    Views:
    1,355
    E. Robert Tisdale
    Aug 24, 2005
  4. LuB
    Replies:
    6
    Views:
    442
    Mike Wahler
    Sep 23, 2005
  5. Tricky
    Replies:
    0
    Views:
    559
    Tricky
    Mar 19, 2009
Loading...

Share This Page