Home > Back-end >  Why is a pointer used in this program instead of regular variables?
Why is a pointer used in this program instead of regular variables?

Time:09-22

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;
}

CodePudding user response:

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.

CodePudding user response:

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().

CodePudding user response:

Pointers are kinda confusing

Then don't use pointers! In C , you can often get away without them by using references instead.

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;
   cin >> a >> b;
   update(a, b);
   return 0;
}

Note that neither your original code nor the code above changes a or b so you really don't need pointers or references for what you've shown; the following will work just fine:

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

Incidentally, using namespace std; is usually a bad idea.

  • Related