-7
#include<iostream>
using namespace std;

void HardToFollow(int *p, int q, int *num);


void HardToFollow(int *p, int q, int *num) {


*p = q + *num;  
  *num = q;    
  num = p;  
  p = &q;  
  cout << *p << " " << q << " " <<*num<<endl;

// value is ``1 1 4
}

main() {

int *q;
  int trouble[3];

  trouble[0] = 1;

  q = &trouble[1];

  *q = 2;

  trouble[2] = 3;

HardToFollow(q, trouble[0], &trouble[2]); // 2 1 3 

cout << *q << " " << trouble[0] << " " << trouble[2]<<endl;  

// value become 4 1 1}

Hi everyone i am beginner to stackoverflow and I really don't get why my first output in HardToFollow function, the value is 1, 1 and 4.

But when it comes to main function output it become 4, 1 and 1. I spent much time try to understand but I can't.

Hopefully someone can help me here.

Roman Cherepanov
  • 1,639
  • 2
  • 24
  • 44
  • 3
    Use a debugger. –  Oct 21 '17 at 15:15
  • 2
    In addition to stepping through your code, with a debugger, I would suggest you read a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Statements `num = p; p = &q;` modifies only the local copies of variables, and such changes are not preserved once the function returns. – Algirdas Preidžius Oct 21 '17 at 15:18
  • 1
    @AlgirdasPreidžius, the code is fine. It's clearly an "hard to follow" example an instructor provided to force the student to understand why the two outputs are different. I believe the instructor wouldn't want the student to ask the explanation on stackoverflow. – Costantino Grana Oct 21 '17 at 15:23
  • 1
    @CostantinoGrana Where I stated that it wasn't? I stated the reason why observed output is different. Please re-read the code, and my comment. – Algirdas Preidžius Oct 21 '17 at 15:26
  • @AlgirdasPreidžius thx for that ! but i dont get it why when the function return the value of *q in main function is 4 . – Perfect isshit Oct 22 '17 at 01:33
  • @Perfectisshit What, **exactly**, you don't understand about it (why do you think, that value you receive should be different)? I already explained it in my comment. Re-read the code, and then, re-read the comment. – Algirdas Preidžius Oct 22 '17 at 11:33
  • @AlgirdasPreidžius okay i understand it now. took me some time cause i think my basic of c++ is not good~ anyway thx for that! – Perfect isshit Oct 22 '17 at 12:58

1 Answers1

0

Let's look at what HardToFollow() receives and follow its flow line-by-line.

[I'll just note right here that this code is, in fact, very hard to follow, so it's possible I've made a mistake. Please follow my logic carefully to see if you agree.]

At the start of HardToFollow():

  • q-in-main = address of trouble[1] (value = 2)
  • p = address of trouble[1] (value = 2)
  • q-in-HardToFollow = copy of value of trouble[0] = 1
  • num = address of trouble[2] (value = 3)

Note that p and q-in-main are not the same variable. They are two separate pointers that point to the same location. This is because (as I'm sure is one of the points of this exercise): Variables, including pointers are passed as copies. So if you send a pointer, a new copy of the pointer is created.

*p = q + *num;

The value of the location pointed to by p is set to q-in-HardToFollow plus the value of the location pointed to by num. This is 1 plus 3, which is 4. q-in-main also points to the same location as p, so its value will also change. So now:

  • q-in-main = address of trouble[1] (value = 4)
  • p = address of trouble[1] (value = 4)

*num = q;

The value of the location pointed to by num is set to q-in-HardToFollow, which is 1. So now:

  • num = address of trouble[2] (value = 1)

num = p;

num is now set to point to the same location as p, that is, trouble[1]. So:

  • num = address of trouble[1] (value = 4)

p = &q;

p is now set to point to the location of q-in-HardToFollow. So:

  • p = address of q-in-HardToFollow (value = 1)

So the current addresses and values are:

  • q-in-main = address of trouble[1] (value = 4)
  • p = address of q-in-HardToFollow (value = 1)
  • q-in-HardToFollow = copy of value of trouble[0] = 1
  • num = address of trouble[1] (value = 4)

This works out well with the output you are getting.

Eli
  • 693
  • 5
  • 12
  • thx for that !! i really dont know when passing a pointer to another function is actually creating a copy of the original pointer. it explain well ! – Perfect isshit Oct 22 '17 at 12:56