pointers-memory · intermediate · ~15 min

Bounds-checked element pointer

Return a valid pointer only when the index is in range.

Challenge

Implement const char *char_at(const char *s, int i) returning a pointer to character i of s if 0 <= i < length, else NULL — so callers never dereference out of range.

Starter code

#include <string.h>
#include <stddef.h>

const char *char_at(const char *s, int i) {
    /* TODO */
    return NULL;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.