Simvik

Return two results

Use pointer parameters to hand back more than one value. An easy embedded C problem (A first look at pointers), run and graded in the browser.

easy · A first look at pointers · pointers · out-parameters

return can hand back only one value. To give the caller two results, a function takes the addresses of two variables and fills them in.

Store the smaller of a and b in *smaller and the larger in *larger.

Starter code

void order(int a, int b, int *smaller, int *larger) {
}

Example tests

Out of order

int lo = 0, hi = 0;
order(9, 4, &lo, &hi);
printf("%d %d\n", lo, hi);

Expected output:

4 9

Already in order

int lo = 0, hi = 0;
order(1, 2, &lo, &hi);
printf("%d %d\n", lo, hi);

Expected output:

1 2

Learn it first

More A first look at pointers problems