34. Double Pointer

#include <stdio.h>

// Function to reassign pointer based on whether value is even or odd
void reassign_based_on_value(int **pp, int *n2_ptr) {
    if (**pp % 2 == 0) {
        *pp = n2_ptr; // If value is even, reassign to n2
    }
    // If odd, keep original pointer (do nothing)
}

int main() {
    int n1, n2;
    scanf("%d %d", &n1, &n2);

    int *p = &n1;

    reassign_based_on_value(&p, &n2);

    printf("%d", *p);

    return 0;
}
  • Pointer p initially points to n1.
  • We pass address of p (&p) and address of n2 to the function.
  • Inside function:
    • If *p (value) is even, reassign p to point to n2.
    • Else do nothing (keep pointing to n1).
  • Finally, print the value pointed by updated pointer.

 

Loading...

Input

10 50

Expected Output

50