Pointers in C

Sort by

recency

|

673 Discussions

|

  • + 0 comments
    #include <stdio.h>
    void update(int *a,int *b) {
        // Complete this function    
         int sum,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;
    }
    
  • + 0 comments

    Addition and subtraction using pointers

    I included <stdlib.h> to use abs() function. Here you can see my code snippet for your reference. If you have any idea feel free to comment.

    #include<stdio.h>
    #include<stdlib.h>
    
    void update(int *a,int *b) {
        //declaring temporary variable to store the values
        int c,d;
        c = *a +*b;  
        d = *b - *a;
    
        //assigning value to pointer variable
        *a = c;
        *b = d;
    }
    
    int main() {
        int a, b;
        int *pa = &a, *pb = &b;
        
        scanf("%d %d", &a, &b);
    
        //function calling
        update(pa, pb);
       
    //using abs() to return positive integer according to requirement
     printf("%d\n%d", a, abs(b));
    
        return 0;
    }
    
  • + 0 comments

    include

    include

    void update(int *a,int *b) { *a=*a + *b; *b=abs(*a - *b); // Complete this function
    }

    int main() { int a, b; int *pa = &a, *pb = &b;

    scanf("%d %d", &a, &b);
    update(&a, &b);
    printf("%d\n%d", a, b);
    
    return 0;
    

    }

  • + 0 comments
    #include <stdio.h>
    
    void update(int *a,int *b) {
        *a = *a+*b;
        *b = abs(*a-*b-*b);
        //The extra *b is because *a value is changed above by adding *b
        //So to equalize the effect we subtract *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;
    }
    
  • + 0 comments

    hiii