Learn C
#include "pointersmemory/const_pointers.h"

const with Pointers in C

#concept

const combines with pointers in three distinct ways, read right-to-left: const char *p is a pointer to const data (can't write through p, can repoint), char * const p is a const pointer to mutable data (can write, can't repoint), and const char * const p locks both. Mixing them up is the most common const bug in C.

String literals demand const char* (writing through them is undefined behavior), read-only function parameters take const pointers (letting callers pass const and non-const alike), and const-correct APIs document exactly what may change.

#how it works
1

Read right-to-left

const char *p: pointer to const char. char * const p: const pointer to char. Position relative to * decides everything.

2

Literals need pointer-to-const

char *p = "hi"; warns (deprecated conversion) — const char *p = "hi"; is correct since literals are read-only.

3

Const params accept all

void f(const char *s) takes const and non-const arguments alike — the maximally reusable read-only signature.

4

Casting const away is a promise

Writing through a cast-away-const pointer is UB if the object is truly const — only do it when you know the origin is mutable.

#example
#include <stdio.h>
 
size_t mylen(const char *s) {
size_t n = 0;
while (*s++) ++n;
return n;
}
 
int main(void) {
printf("%zu\n", mylen("hello"));
return 0;
}
Output
5
1mylen takes a pointer-to-const — it can read the literal but never modify it, enforced by the compiler.
2The local pointer itself advances (*s++) while the pointed-to data stays read-only — the two const positions distinguished.
35 prints — five characters traversed without a single write.
#use cases
String parameters as const char* throughout APIs
Read-only buffer traversal that the compiler verifies
Fixing deprecated string-literal conversion warnings
Auditing cast-away-const sites for genuine mutability

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.