Using Pointers as Function Arguments

Pointers as function arguments can be used to pass values back to the caller.

This is convenient when multiple values must be passed back.

Its better style to use return to pass a single value back.

So this example is poor style:


#include <stdio.h>

void
findMax(int x, int y, int *bigger) {
    if (x > y)
        *bigger = x;
    else
        *bigger = y;
}

int
main(void) {
    int a, b, c;
    
    printf("Enter two numbers: ");
    scanf("%d%d", &a, &b);
    findMax(a, b, &c);
    printf("The larger is %d\n", c);
    
    return 0;
}

Index