Example Pointer in Functions

Hello,
I’m dealing with the pointers part (I have nightmares from some years ago when I first saw it on the university, and here I am again hehe).

In the Pointer in Functions section, there’s an example using pointers inside functions, but I have doubts with this one, here is the code used:

#include <iostream>
using namespace std;

void increment(int &number) {
    cout << "The address of number is: " << &number << endl;
    number++;
    cout << "Now the value of number is: " << number << endl;
}

int main() {
    
    // Declare the number
    int number = 42;
    // Print the number's value
    cout << " The value of number is: " << number << " and the address is: " << &number << endl;
    // Call the function increment
    increment(number);
    
    
    return 0;
}

Now my question is:

  • I’m passing the variable name, but then where I define the function, there’s an argument which seems to refer to the adress (&number) of the previous variable, but correct me if I’m wrong, isn’t this just the way we name the argument is going to use as a variable inside the function?
    So what’s the point of it and not saying:
void increment (number){...

I mean, if I define the variable in the function as &number, when I passed the variable “number”, shouldn’t &number == number?? It is hard for me to explain it, but I think this might be an issue, because normally when we use &<name_variable> we are refering to the adress, does C++ do the stuff for us?

For me it was more clear if I just defined the function with the variable as its name and then I get the adress using the symbol &.

I changed void increment(int &number) to void increment(int number) and the program works the same.

Hope you can clarify me these aspects.
Have a great day,
Ángel

Hi @ACTISA,

You said that if you use void increment(int number) instead of void increment(int &number) works the same way. This information is almost true because there is a subtle thing happening that changes everything, which is:

  • When you use pointers or references, the value is changed in the original variable inside the main function. When you don’t use pointers/references, the value is changed locally.

  • Now, imagine you have a huge video to edit. Would you want to have a copy of the variable, which would consume a lot of memory, or would you like to modify the video in the original variable, saving resources, and not having to return the modified video?

Taking your code as an example, here I have a modified version of it:

#include <iostream>
using namespace std;

void incrementByReference(int &number) {
    cout << "\nref:: The address of number is: " << &number << endl;
    number++;
    cout << "ref:: Now the value of number is: " << number << endl;
}

void incrementByPointer(int *number) {
    cout << "\npointer:: The address of number is: " << number << endl;
    (*number)++;
    cout << "pointer:: Now the value of number is: " << *number << endl;
}

void incrementByValue(int number) {
    cout << "\nvalue:: The address of number is: " << &number << endl;
    number++;
    cout << "value:: Now the value of number is: " << number << endl;
}

int main() {

    // Declare the number
    int number = 42;
    // Print the number's value
    cout << "The initial value of number is: " << number << " and the address is: " << &number << endl;

    // Call the function incrementByReference
    // This will change the value in the main function
    incrementByReference(number);

    // Calling incrementByPointer will also change the value in the main function
    incrementByPointer(&number);

    // incrementByValue changes the value only insde the incrementByValue function itself.
    // The value on the main function keeps untouched
    incrementByValue(number);

    cout<<"\n main::Now the value of 'number' at 'main' function: " << number <<endl;

    return 0;
}

If you compile this code and execute it, you will see something similar to the following:

The initial value of number is: 42 and the address is: 0x7ffe4738fe84

ref:: The address of number is: 0x7ffe4738fe84
ref:: Now the value of number is: 43

pointer:: The address of number is: 0x7ffe4738fe84
pointer:: Now the value of number is: 44

value:: The address of number is: 0x7ffe4738fe6c
value:: Now the value of number is: 45

 main::Now the value of 'number' at 'main' function: 44

Please analyze the modified code I posted, and the output until you really understand it. Understanding this will definitely make your life easier.

How much better to get wisdom than gold,
to get insight rather than silver!
- Proverbs 16:16

1 Like