Simvik

Return a pointer

Hand back the address of an element, or NULL when there is none. An easy embedded C problem (A first look at pointers), run and graded in the browser.

easy · A first look at pointers · pointers · arrays · null-pointer

Return the address of the first element among the first n elements of values that equals target. If there is none, return NULL.

&values[i] is the address of element i.

Starter code

int *find_value(int values[], int n, int target) {
  return NULL;
}

Example tests

Found

int v[] = { 5, 6, 7 };
int *p = find_value(v, 3, 6);
printf("%d %d\n", (int)(p - v), *p);

Expected output:

1 6

Not found

int v[] = { 5, 6, 7 };
printf("%s\n", find_value(v, 3, 9) == NULL ? "not found" : "found");

Expected output:

not found

Learn it first

More A first look at pointers problems