Simvik

Advance a cursor

Move a caller's string pointer past leading whitespace. A medium embedded C problem (Pointers), run and graded in the browser.

medium · Pointers · double-pointer · parsing

A command parser keeps a cursor into the line it is reading. Helper functions advance that cursor, so they receive a pointer to it.

Advance *cursor past any spaces and tabs. Stop at the first other character, or at the terminating NUL.

Starter code

void skip_spaces(const char **cursor) {
  // Move *cursor forward while it points at ' ' or '\t'.
}

Example tests

Leading spaces

const char *s = "   AT+GMR";
const char *p = s;
skip_spaces(&p);
printf("%d [%s]\n", (int)(p - s), p);

Expected output:

3 [AT+GMR]

Nothing to skip

const char *s = "OK";
const char *p = s;
skip_spaces(&p);
printf("%d [%s]\n", (int)(p - s), p);

Expected output:

0 [OK]

Learn it first

  • Pointers · Addresses, arithmetic, const, void pointers and object lifetime.

More Pointers problems