0

Can anybody please explain how this code functions? Pointers are kinda confusing so I need a little bit of help to understand what's happening. I know that it's calculating the sum and absolute difference but I don't get why we need pointers here. Please help.

#include <iostream>

using namespace std;

void update(int *a, int *b) {    
   int sum = *a + *b;
   int abs = *a - *b;
   if (abs < 0) abs *= -1;
   cout << sum << '\n' << abs;
}

int main() {
   int a, b;
   int *pa = &a, *pb = &b;
   cin >> a >> b;
   update(pa, pb);
   return 0;
}
NerdNet
  • 359
  • 1
  • 3
  • 11
  • 5
    There is no need for pointers here. Where did you find this? Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Sep 22 '21 at 12:59
  • If you remove all the `*` from the `update` function, and just pass in `a` and `b` into the function directly, do you understand what it is doing then? – ChrisMM Sep 22 '21 at 12:59
  • 2
    There is no need at all here, it is just a pointless illustration. – molbdnilo Sep 22 '21 at 12:59
  • 1
    I'd guess that your instructor is getting around to explaining why passing parameters **by reference** may be better than passing them **by value** in some cases. But this is not one of those cases. – Tim Randall Sep 22 '21 at 12:59
  • 4
    *...I don't get why we need pointers here...* We don't need pointers here. There's nothing to get. Your insight is correct. – Eljay Sep 22 '21 at 13:01
  • Pointers are very simple. A pointer tells you where a thing is; `&x` is the location of the thing `x`; `*p` is the thing in the location `p`. (If you think "home address", "where does this person live?" and "who lives in this house?", you have an often useful analogy.) – molbdnilo Sep 22 '21 at 13:06
  • @molbdnilo I get that but what is the point of using pointers in this code? – NerdNet Sep 22 '21 at 13:09
  • There is no point of using them in this piece of code, since update doesn't change the values of a and b. So in this case (non-pointer) int parameters would work just as well. In the case a and b would be modified you would be better of with references anyway. – Pepijn Kramer Sep 22 '21 at 13:15
  • @NerdNet There is no point whatsoever, unless it is accompanied by an explanatory text. – molbdnilo Sep 22 '21 at 13:17
  • 2
    @molbdnilo What is this fascination of teachers with pointers anyway? For C++ they really should be teaching by value and by reference passing first. Oh well... – Pepijn Kramer Sep 22 '21 at 13:18
  • You should use pointers in a case only when you NEED to use them. This example looks like from a bad c++ book, which tried to illustrate how pointers and dereferencing work. You need to use pointers for example in polymorphism. – Karen Baghdasaryan Sep 22 '21 at 13:54

2 Answers2

4

I don't get why we need pointers here

We don't. This piece of code is needlessly convoluted. My best guess is your teacher is building up to something they haven't shown you yet.

The use of pointers here is pointless. No pun intended. You could do exactly the same thing without pointers.

That doesn't mean pointers are useless. They are very important. But that piece of code is just... not suited to show their importance.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
3

You're right. This is confusing. The code is needlessly difficult, in more ways than one.

Let's look at what's happening. The function update(int *a, int *b) takes two pointers to integers as parameters. That is, when you call it, you pass the addresses of two variables that are ints. The function adds and subtracts the actual integers, using the * operator to dereference the pointers, meaning, to read the actual variables that a and b are pointing at.

There's no need to pass the parameters as pointers, and the first change I'd make to this code is to pass the variables by value. All that update() needs to know is the values, not where the variables are in memory. (If it needed to change the values of those variables, that'd be different.) The only result of calling the function is the printing of the sum and absolute difference between the values, which is why the second thing about the code that I'd change is the name of this function.

Looking at main() we see that the parameters passed are indeed the addresses of two variables, which are obtained using the address-of & operator. Note that the variables defined in main() are given the same name as the parameters in update(), which is potentially confusing since they are quite different. In one case they are integers; in another, pointers to integers. But even if they were the same type, they would refer to different variables in different places in memory, and a programmer might confuse the two. So the last change I'd make is rename the variables.

void print_sum_difference(int i1, int i2) {    
   int sum = i1 + i2;
   int abs = i1 - i2;
   if (abs < 0) abs *= -1;
   cout << sum << '\n' << abs;
}

int main() {
   int a, b;
   cin >> a >> b;
   print_sum_difference(a, b);
   return 0;
}

Having said all that... why were pointers used? I'm guessing that the code in question is a step on the road to using "pass by reference" mechanisms to allow a function to modify values in the calling code. When we pass by value, as in my code above, the function is given a copy of the variables. Two new integers are made, the values are copied, and the function gets to use those. If it changes them, the variables in main() aren't changed. But in the original code, the values could be changed by code in update() like *a=0; which would change the value of a in main().

Tim Randall
  • 4,040
  • 1
  • 17
  • 39
  • Thanks for the detailed explanation. I think I sorta get it but It's still pointless to use pointers in this specific program like you said as well. – NerdNet Sep 22 '21 at 13:51