What challenge you will learn from the problem

  • This solution will help you To learn how to use pointers you will learn to implement the basic functionalities of pointers in C. A  Pointer in C is a way to share a memory address among different contexts (primarily functions).

Pointer In c Hacker Rank Solutions 
                 



Explanation

        About-Problem


  • In order to access the memory address of a variable, For example, &val returns the memory address of .
  • This memory address is assigned to a pointer and can be shared among various functions. For example, we  will assign the memory address of  to pointer .
  •  To access the content of the memory to which the pointer points, depend  with a *. For example, *p will return the value reflected by  and any modification to it will be reflected at the source ().



Given question

  • Complete the function void update(int *a,int *b). It receives two integer pointers, int* a and int* b. Set the value of  to their sum, and  to their absolute difference. There is no return value, and no return statement is needed.


Absolute difference means 
 

Absolute difference means after the subtraction there will be no negative sign.
diff=abs(*a-*b);



Coding


#include <stdio.h>
#include<stdlib.h>
void update(int *a,int *b)
{
    int sum=0,diff;
    sum=*a+*b;
    diff=abs(*a-*b);
   *a=sum;
   *b=diff;
}


int main() {
    int a, b;
    int *pa = &a, *pb = &b;
    
    scanf("%d %d", &a, &b);
    update(pa, pb);
    printf("%d\n%d", a, b);


    return 0;
}
 

Output

your input=
4
5
your output=
9
1

Explanation Of code

  • First of all we input the header files.
  • Then we make a function of name update with no return type and take two arguments(references).
  • Then inside the function we declare the sum=0,diff and do the calculation for the sum and difference.
  • and in the difference we have to do abstract difference so that we use (abs) keyword for this difference.
  • Then in the main function we declare to variable and store the adress in the pointer then pass the values to the function update and return 0; program ends. 
 

Also check Out





  Q-2)  pointers in c




  

   Q-3) Vector Sort


Also check Out Projects for Resume


Projects on C++


Projects on C

Feedback!!

If you enjoyed this post, share it with your friends. If you have any issue Let us know in the comments. Thank you!!