The one-page companion to lessons on pointers, arrays, and string handling.
| Declaration | What you get | Writable? |
|---|---|---|
char *p; |
A pointer variable (8 bytes on 64-bit). It holds an address โ and that's all. No characters exist. Uninitialized โ garbage address. | DANGER Dereferencing is undefined behavior (UB). |
char buf[100]; |
100 real chars of storage on the stack. buf "decays" to char* in most expressions, but the storage is real. |
SAFE Write into buf (up to 100 bytes). |
char *p = buf; |
A pointer that now points at real storage. Best of both: pointer flexibility, real backing. | SAFE |
char *s = "hi"; |
A pointer to a string literal stored in read-only memory. | READ-ONLY Writing through s is UB. Use const char *. |
char *s; // points nowhere
scanf("%s", s); // UB: writes chars to a garbage address
char buf[100];
scanf("%99s", buf); // at most 99 chars + '\0' โ never overflows
Why %99s? The width means "read at most 99 characters"; scanf appends the '\0', totaling 100. Always size the width to buffer_size โ 1.
char buf[100];
fgets(buf, sizeof buf, stdin); // reads a line, spaces and all
// note: keeps the trailing '\n' โ strip it if you don't want it
buf[strcspn(buf, "\n")] = '\0';
fgets reads at most sizeof buf โ 1 chars and always null-terminates. It is the safe, boring, correct default.
<string.h> โ the functions, and what bites you| Function | Does | Watch out |
|---|---|---|
strlen(s) |
Returns length, excluding the '\0'. |
Argument must be null-terminated. Returns size_t, not int. |
strcpy(dst, src) |
Copies src into dst, including '\0'. |
OVERFLOW If src is bigger than dst's storage โ UB. dst must not overlap src. |
strcat(dst, src) |
Appends src to the end of dst. |
OVERFLOW dst must have room for strlen(dst) + strlen(src) + 1. A common, silent bug. |
strstr(hay, needle) |
Returns pointer to first needle in hay, or NULL. |
Check for NULL before using the result. |
strcmp(a, b) |
Compares two strings. Returns 0 if equal. | Returns 0 for equal โ easy to write if (strcmp(...)) by mistake. Use == 0. |
| Region | What's there | Lifetime |
|---|---|---|
| Stack | Local variables, function args. | Until the function returns. Pointers to these go stale after return. |
| Static / global | Globals, static locals, string literals. | The whole program. String literals are read-only. |
| Heap | malloc/calloc/realloc memory. | Until you free it. You own it. |
scanf / strcpy / strcat'\0'.%99s, or use fgets.)