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,valueis1→&data[2] - numbers
4 7 1 7 9,valueis7→&data[1] - numbers
4 7 1 7 9,valueis5→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.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.