All submissions

Add Two Integers Using Void Pointers

Code

#include <stdio.h>


int add_two_void_pointers(void *a, void *b) {
    // Your logic here
    int val1 = *(int*)a;
    int val2 = *(int*)b;

    return val1 + val2;
}

int main() {
    int x, y;
    scanf("%d %d", &x, &y);

    int result = add_two_void_pointers(&x, &y);
    printf("%d", result);

    return 0;
}

Solving Approach

/*
Questions:
1. should the function handle null pointers?
2. Input is always of type int or any other types?
3. How to handle integer overflow?
4. function is expected to work little-endian or both little-big endian

Plan:
1. Accept two void pointers as func arg
2. cast each void pointer as an int pointer
3. Deref each pointer to get the int value
4. Add the two int
5. return the sum
*/

 
Loading...

Input

10 20

Expected Output

30