Sunday, January 5, 2020

Pointers in C - Hacker Rank Solution

Objective
In this challenge, 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). They are primarily used whenever a function needs to modify the content of a variable, of which it doesn't have ownership.

 C - Hacker Rank Solution

#include <cstdio>
#include <cstdlib>

void update(int *a,int *b) {
    int temp = *a;
    *a = *a + *b;
    *b = abs(temp - *b);

}

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

9 comments:

  1. run time error bata rha hy code aapka

    ReplyDelete
  2. Replies
    1. absolute function which actually works as modulas

      Delete
  3. it's not getting executed compilation error de raha

    ReplyDelete
    Replies
    1. #include
      void sol(int *,int *);
      int main(){
      int a,b;
      scanf("%d%d",&a,&b);
      sol(&a,&b);
      printf("%d\n%d",a+b,abs(a-b));
      return 0;
      }
      void sol(int *x,int *y){
      *x+*y;
      abs(*x-*y);
      }

      Delete
    2. ye wala kro execute hoga main khud se kiya hoo with the help of abs use

      Delete
  4. #include
    #include


    void update(int *a,int *b) //a=&a; b=&b; a,b is a pointer
    /*
    nt *a;
    a=pa;
    a=*a;
    *a=a
    int *b;
    b=pb;
    b=*b;
    *b=b;
    */



    {
    int temp=*a;
    *a =(*a+*b);
    *b=int(abs(temp-*b));


    }

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

    ReplyDelete

Powered by Blogger.