Skip to content
Exercise · References and Pointers · lesson 8 of 8

Exercise: find an element, return a pointer

Write a function const int* find_value(const int* data, int n, int value). The pointer data points to the first of n numbers that lie in memory one after another. The function returns a pointer to the first of these numbers that equals value, or nullptr if there is no such number.

  • numbers 4 7 1 7 9, value is 1 → &data[2]
  • numbers 4 7 1 7 9, value is 7 → &data[1]
  • numbers 4 7 1 7 9, value is 5 → nullptr

Constraints: the value can be at any position and occur more than once; n may be 0, and then the function returns nullptr.

find_value.cpp — cpp
cpp · 4 lines
Ctrl+Enter run · Esc leave editor