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

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.

Do not add extra #include lines unless the task says so explicitly.

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

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way