Skip to content

← Orientation and the Gate step 1 of 3

Easy Primitives

Your first C++ lint check

The starter finds the correct element, but uses NULL to represent a missing result. Replace it with nullptr to satisfy the C++ lint check. This small change introduces the three checks used to grade C++ submissions here.

How grading works

A submission must pass all three checks:

  1. Compilation: the runtime builds your code as C++20.
  2. Tests: your function must produce the expected results.
  3. Static analysis: clang-tidy checks your submitted code for potential bugs and selected style and performance issues.

Diagnostics in the supplied harness do not count against your lint result. If the runtime cannot run clang-tidy, the submission cannot be graded as solved. Passing all three checks is useful evidence, but does not prove that a program has no bugs.

Read the diagnostic

For the starter's return NULL;, expect a message like this:

warning: use nullptr [modernize-use-nullptr]
    return NULL;
           ^~~~
           nullptr

The message suggests a replacement, and the name in brackets identifies the check. Exact formatting may vary with your installed version of clang-tidy.

NULL is a valid null pointer constant, so it does not make this function's result incorrect. Its definition depends on the implementation and may be an integer zero. nullptr expresses the intent directly: its type is std::nullptr_t, and it converts to pointer types without converting to int. For example, given overloads that accept int and int*, passing 0 chooses the integer overload while passing nullptr chooses the pointer overload.

The modernize-use-nullptr check recommends this replacement for null pointer constants.

Your task

const int* find_first(const std::vector<int>& xs, int target)

Replace NULL with nullptr. Keep the existing search logic:

  • Return a pointer to the first element equal to target.
  • Return a null pointer if there is no match, including when xs is empty.
  • Treat zero and negative values like any other integers.

The returned pointer borrows an element of xs; it does not own that element. The caller must keep the vector alive and avoid operations that invalidate the pointer while using it. In the starter, x is a reference to an element, so &x points into the vector. Changing it to a local copy would make &x point to an object that no longer exists after the function returns.

How results are shown

The supplied harness calls your function and reports the element's zero-based index, or -1 for a null pointer. You return the pointer, not the index, and do not need to write main.

For xs = [4, 8, 15, 16] and target = 15, return a pointer to xs[2]; the displayed result is 2. With target = 99, return nullptr; the displayed result is -1.