4

I am currently taking a data structures and algorithms class and my professor gave us material that included functions that take in pointer values and pointer/reference values.

These functions would look like this:

int function1(int a); // Pass by value
int function2(int &ref); // Pass by reference

int function3(int* ptr); // This will take in a pointer value
int function4(int*& ptr); // This will take in a pointer/reference value

I understand the difference between pass by value, and pass by reference. I also have tried implementing both of the latter two examples as basic functions, but I am not entirely sure how these two arguments differ from pass by reference or how they differ from each other.

Could somebody explain how these two functions parameters work and how they could be used practically?

2
  • 1
    The first one is pass-by-value, the second one is pass-by-reference Commented Dec 18, 2019 at 6:31
  • Note that the second is a reference to a pointer Commented Dec 18, 2019 at 6:32

4 Answers 4

7

[...] but I am not entirely sure how these two arguments differ from pass by reference or how they differ from each other.

In the first function

int function3(int* ptr);
//            ^^^^ 

you pass the pointer to an int by value. Meaning int* by value.

In second one,

int function4(int*& ptr);
//               ^^ --> here

you pass the pointer to the int by reference. Meaning you are passing the reference to the int* type.


But how does passing the pointer by value and by reference differ in usage from passing a regular variable type such as an integer by value or reference?

Same. When you pass the pointer by value, the changes that you do the passed pointer(ex: assiging another pointer) will be only valid in the function scop. On the otherside, pointer pass by reference case, you can directly make changes to the pointer in the main(). For example, see the folloiwng demonstration.

#include <iostream>    
// some integer
int valueGlobal{ 200 };

void ptrByValue(int* ptrInt)
{
    std::cout << "ptrByValue()\n";
    ptrInt = &valueGlobal;
    std::cout << "Inside function pointee: " << *ptrInt << "\n";
}

void ptrByRef(int *& ptrInt)
{
    std::cout << "ptrByRef()\n";
    ptrInt = &valueGlobal;
    std::cout << "Inside function pointee: " << *ptrInt << "\n";
}

int main()
{
    {
        std::cout << "Pointer pass by value\n";
        int value{ 1 };
        int* ptrInt{ &value };
        std::cout << "In main() pointee before function call: " << *ptrInt << "\n";
        ptrByValue(ptrInt);
        std::cout << "In main()  pointee after function call: " << *ptrInt << "\n\n";
    }
    {
        std::cout << "Pointer pass by reference\n";
        int value{ 1 };
        int* ptrInt{ &value };
        std::cout << "In main() pointee before function call: " << *ptrInt << "\n";
        ptrByRef(ptrInt);
        std::cout << "In main()  pointee after function call: " << *ptrInt << "\n\n";
    }
}

Output:

Pointer pass by value
In main() pointee before function call: 1
ptrByValue()
Inside function pointee: 200
In main()  pointee after function call: 1

Pointer pass by reference
In main() pointee before function call: 1
ptrByRef()
Inside function pointee: 200
In main()  pointee after function call: 200
Sign up to request clarification or add additional context in comments.

2 Comments

Okay thank you this makes more sense. But how does passing the pointer by value and by reference differ in usage from passing a regular variable type such as an integer by value or reference?
try swapping of two variables using those functions (dont return anything) - its a classic example to make you understand.
3

If you want to modify an outside int value, use this:

int function1(int* ptr) {
    *ptr = 100; // Now the outside value is 100
    ptr = nullptr; // Useless. This does not affect the outside pointer
}

If you want to modify an outside int* pointer, i.e. redirect that pointer, use this:

int function2(int*& ptr) {
    ptr = nullptr; // Now the outside pointer is nullptr
}

Or this:

int function3(int** ptr) {
    *ptr = nullptr; // Same
}

Comments

2

Passing a pointer by reference allow you to change the pointer and not only the pointed value. So if you do and assignment of pointer in the function int function4(int*& ptr) in this way :

ptr = nullptr;

the caller have the pointer set to "nullptr".

In the function int function3(int* ptr) the pointer passed is copied in the variabile ptr that is valid only in the scope of function3. In this case you can only change the value pointed by the pointer and not the pointer ( or to better specify you can change the pointer but only in the scope of the function ). So the previous expression ptr = nullptr; has no effect in the caller.

Comments

1

The first and second are pass by value and reference respectively as commented in the code.

The third one takes an integer pointer as a parameter by value, which can be assigned a memory location but the value of the pointer only changes locally inside the function scope.

The fourth one holds the value of an integer variable with reference as there is an amperson(&) for referencing the value(which is a pointer).

They are function declarations, not definitions so unless they are defined with a function body you can't explore its practical uses. (or a list of endless possibilities from what you could do with an integer value) If you want a case of pass by value vs pass by reference, the classic example would be the swapping of two or more values within a function (without returning values), wherein pass by reference works and pass by value fails or your first and third functions would change the values locally inside the function only whereas the second and fourth ones would change the value in its entirety.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.