Pointers & Memory · beginner · ~10 min
Walk strings via pointer increment.
A char * and a char[] decay to the same thing when passed around. Many string library functions are most natural to implement as pointer walks: increment until you hit \0.
const char * declares "the pointed-to chars are read-only". char *const declares "the pointer itself can't be reassigned". Both are useful in APIs to document intent.
size_t count_a(const char *s) {
size_t n = 0;
for (; *s; s++) if (*s == 'a') n++;
return n;
}
char *p = "hi"; p[0] = 'H'; — segfault on most systems.const: a function that doesn't write should declare const char *.