Simvik

Pointer to the largest element

Return a pointer to the maximum of an array, or NULL when it is empty. An easy embedded C problem (Pointers), run and graded in the browser.

easy · Pointers · arrays · null-pointer

Return a pointer to the largest element of the n samples at data. If the maximum occurs more than once, return the first occurrence. If n is 0, return NULL.

Starter code

const int16_t *find_max(const int16_t *data, size_t n) {
  // Return a pointer to the largest element, or NULL for an empty array.
  return NULL;
}

Example tests

Maximum appears twice

int16_t d[] = { 3, 9, -2, 9, 5 };
const int16_t *p = find_max(d, 5);
printf("%d %d\n", (int)(p - d), *p);

Expected output:

1 9

Single element

int16_t d[] = { -4 };
const int16_t *p = find_max(d, 1);
printf("%d %d\n", (int)(p - d), *p);

Expected output:

0 -4

Learn it first

  • Pointers · Addresses, arithmetic, const, void pointers and object lifetime.

More Pointers problems