Functions & Pointers

Hello , I was trying to solve a problem that I was facing while trying to understand pointers correctly and its kind of a mystery to me atm.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

void increment(int x) {
std::cout << "Beginning execution of increment, x = "
<< x << '\n';
x++; // Increment x
std::cout << "Ending execution of increment, x = "
<< x << '\n';
}
int main() {
int x = 5;
std::cout << "Before increment, x = " << x << '\n';
increment(x);
std::cout << "After increment, x = " << x << '\n';
}



Output Screen here is:

1
2
3
4
Before increment, x = 5
Beginning execution of increment, x = 5
Ending execution of increment, x = 6
After increment, x = 5




Now this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

void increment(int *x) {
    int z = *x;
std::cout << "Beginning execution of increment, x = "
<< z << '\n';
++z; // Increment x
std::cout << "Ending execution of increment, x = "
<< z << '\n';
}
int main() {
int x = 5;
std::cout << "Before increment, x = " << x << '\n';
increment(&x);
std::cout << "After increment, x = " << x << '\n';
}


Has the SAME output screen:

1
2
3
4
Before increment, x = 5
Beginning execution of increment, x = 5
Ending execution of increment, x = 6
After increment, x = 5



Meanwhile, this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

void increment(int *x) {

std::cout << "Beginning execution of increment, x = "
<< *x << '\n';
*x++; // Increment x
std::cout << "Ending execution of increment, x = "
<< *x << '\n';
}
int main() {
int x = 5;
std::cout << "Before increment, x = " << x << '\n';
increment(&x);
std::cout << "After increment, x = " << x << '\n';
}


Has this weird output:
1
2
3
4
Before increment, x = 5
Beginning execution of increment, x = 5
Ending execution of increment, x = 7012128
After increment, x = 5


Now my question is, how to correctly use pointers in such a case(with explanation if possible) and what did I do wrong.

AND, why do they ALLLL share the "After increment, x = 5" despite the method used?
Last edited on
Your first function:
1
2
3
4
5
6
7
8
// The parameter x binds to a copy of the value of the argument.
// x is passed by value; there are no pointers or references here.  
// In other words, the x inside `increment()' is different from the x defined in `main()'. 
void increment(int x) { 
  std::cout << "Beginning execution of increment, x = " << x << '\n';
  x++; // Increment our x
  std::cout << "Ending execution of increment, x = " << x << '\n';
}


Your second function:
1
2
3
4
5
6
void increment(int *x) {
  int z = *x; // store a copy of the value pointed to by x. 
  ++z; // increment z, our copy of the value pointed to by x.
  std::cout << "after increment: z has value " << z << '\n';
  std::cout << "after increment: the value pointed to by x is still " << *x << '\n';
}


Your third function is undefined behavior:
1
2
3
4
5
6
7
8
void increment(int *x) {
  std::cout << "Beginning execution of increment: value pointed to by x = " << *x << '\n';
  // undefined behavior:
  // *x++; // Increments the value of x (so that it points to the next int), then dereference.
  // You probably wanted 
  (*x)++; // Increments the value pointed to by x
  std::cout << "Ending execution of increment: value pointed to by x = " << *x << '\n';
}


The all share the "after increment, x = 5" because you never modified the pointed-to-value. You were close to accomplishing this with the third function, except operator precedence bit you.

Please ask if you need clarification.
Last edited on
// *x++; // Increments the value of x (so that it points to the next int), then dereference.

I didn't exactly get what you meant by that sentence and the difference between this and

(*x)++;

and also how do you fix the first function so the x is displayed in main function as it should be ..."incremented"
Last edited on
*x++ is equivalent to *(x++).
The value of x is some memory address. The form above changes the memory address first, and then looks at the value pointed to by that memory address. The problem is that if x is at address 2, nothing's at address 3: the above snippet looks at address 3. (that's why the output was garbage).

Note that in the context you used it above, the dereference itself has no purpose, since the result is ignored. Because the result was ignored in the context you used it, *x++; was equivalent to simply x++;, which changes the memory address (the value of x) and that's it.

(*x)++; Looks at the value pointed to by x (i.e., the value at the memory address x contains) and adds one to it.

and also how do you fix the first function so the x is displayed in main function as it should be "incremented"

Have you learned of "pass by reference" yet?
If you have, you should let the reference parameter x bind to the variable you want to change:
void increment(int& x) { x++; }
Then elsewhere you could call
1
2
3
4
int my_variable  = 0; 
std::cout << "before increment: " << my_variable << '\n'; // prints 0
increment(my_variable);
std::cout << "after increment: " << my_variable << '\n'; // prints 1 



If not, you'd have to accept the address of x and change the value it points to, assuming, of course, that the address you got was valid:
void increment(int* x) { (*x)++; }
Then elsewhere you could call
1
2
3
4
int my_variable  = 0; 
std::cout << "before increment: " << my_variable << '\n'; // prints 0
increment(&my_variable);
std::cout << "after increment: " << my_variable << '\n'; // prints 1 

Last edited on
You're awesome, thank you very much <3
Topic archived. No new replies allowed.