cpp
Pointers Hackerrank Solution

Pointers in C++ HackerRank solution

Pointers are the initial steps towards advancement in the programming mindset. A pointer holds a memory address which can be helpful to solve problems in an alternative way by accessing variables. This tutorial will help you with the pointers in C++ HackerRank solution.

Pointers in C++ HackerRank solution

Problem statement

A pointer in C++ is used to share a memory address among different contexts. They are used whenever a function needs to modify the content of a variable, but it does not have ownership…

You can check out the complete problem on Pointers (opens in a new tab) at HackerRank.

Also Read: Hackerrank problem on For loop

Solution

An update() function is defined which takes two int pointers as arguments. It creates a temp integer and holds the sum of two passed arguments, the second argument **b** holds the subtraction of the pointers. Later the **temp** value is passed onto the **a** pointer.

In this scenario, a and **b** are the pa & pb passed as pointers of the main **a** & b. The **abs**() converts any input to a positive number.

 #include <stdio.h>
#include <iostream>
 
using namespace std;
 
int update(int *a,int *b) {
    int temp;
    temp = *a + *b;
    *b = abs(*a - *b);
    *a = temp;
    return *a, *b;
}
 
int main() {
    int a, b;
    int *pa = &a, *pb = &b;
    cin >> a >> b;
    update(pa, pb);
    cout << a << endl << b;
    return 0;
}

IndGeek provides solutions in the software field, and is a hub for ultimate Tech Knowledge.