Loading…
Loading…
Charting your lesson…
Opening your lesson…
Arrays and Strings in C
Lesson 3 of 4
6 steps•about 11 min•12 flashcards
Step 1/ 61 min
C has no string type. It has arrays of char and a single convention — a terminating '\0' byte — and everything from strlen to buffer-overflow exploits follows from that. Understanding arrays and their decay into pointers is the key.
Core idea: an array is a contiguous block of same-typed elements. A C string is simply a char array whose logical end is marked by a null byte '\0' (value 0), not by its allocated length.
Step 2/ 61 min
int a[5] = {10, 20, 30}; // a[3], a[4] are zero-initialised
Elements sit back-to-back in memory, so a[i] is found by address arithmetic base + i*sizeof(int) — O(1) access. C does no bounds checking: a[7] compiles and runs, reading whatever bytes are there. That silence is the root of countless security bugs.
In almost every expression, an array name decays to a pointer to its first element. So when you pass a to a function, the function receives an int *, not the array — the size information is lost.
void f(int arr[]) { printf("%zu\n", sizeof(arr)); } // prints 8 (pointer!), NOT 20
That is why C functions almost always take a length parameter alongside the array. The two places decay does not happen: operands of sizeof and of &.
char s[] = "hi"; // stored as {'h','i','\0'} — THREE bytes
strlen(s) walks from the start counting until it hits '\0' → returns 2 (the terminator is not counted), an O(n) scan. sizeof(s) is 3 (includes the terminator). Copying with strcpy(dst, src) copies bytes including the terminator but performs no bounds check — if dst is smaller than src, you overflow. Safer: strncpy/snprintf with an explicit limit.
Step 3/ 61 min
char msg[10] = "code"; // bytes: c o d e \0 \0 \0 \0 \0 \0
printf("%zu %zu\n", strlen(msg), sizeof(msg)); // 4 10
strlen counts up to the first '\0' → 4. sizeof reports the allocated array size → 10. Confusing the two is the classic off-by-one/overflow source: you must budget length+1 for the terminator.
Step 4/ 61 min
Favourites: "difference between sizeof and strlen", "why does sizeof(arr) inside a function not give the array size" (decay), "reverse a string in place", and reasoning about char *s = "x"; (string literal, read-only) versus char s[] = "x"; (a modifiable copy). Writing to a string literal is undefined behaviour.
Step 5/ 61 min
length + 1 bytes for a string to hold the '\0'.snprintf over sprintf/strcpy — the n variants take a size bound and prevent overflow.char *p = "hello"; points to read-only memory; modifying p[0] is UB. Use char p[] = "hello"; to get a writable array.Forgetting the null terminator when sizing a buffer. To store the 5-character word "hello" you need char buf[6], not buf[5]. Allocating exactly strlen bytes and then strcpy-ing overruns by one byte — a subtle, corruption-prone bug that may not crash immediately.
sizeof/&).char arrays ending in '\0'; allocate length+1.strlen is O(n) up to the terminator; sizeof is the allocated size.snprintf, strncpy) to avoid overflows.'\0'. Respect decay (pass a length), respect the terminator (allocate +1), and prefer bounded string functions.Step 6/ 6· Worked examples1 min
Problem
Explain the output and the subtle bug.
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "hello";
printf("len=%zu size=%zu\n", strlen(s), sizeof s); // (A)
char *p = "world";
int a[5] = {1, 2};
printf("a2=%d a4=%d\n", a[2], a[4]); // (B)
char dst[4];
strcpy(dst, "abcd"); // (C) danger
printf("%s\n", dst);
return 0;
}
Try it on paper first.
Recall
Keep this
Arrays and C-strings expose subtle memory behaviour — array-to-pointer decay, why sizeof differs inside a function, and the null terminator. These are heavily tested in written rounds because a single off-by-one causes a buffer overflow.
An array is a contiguous block of same-type elements. The name a usually decays to a pointer to its first element (&a[0]) — but not under sizeof, &, or in a string literal initializer. So sizeof(a) inside main gives the whole array size, but a function receiving int a[] only sees a pointer.
A C-string is a char array terminated by a null character '\0'. "hello" occupies 6 bytes (5 letters + terminator). Standard functions rely on that terminator to know where the string ends.
| Function | Purpose | Note |
|---|---|---|
strlen |
length | excludes '\0' |
strcpy |
copy | no bounds check |
strcmp |
compare | 0 if equal |
strcat |
concatenate | destination must have room |
strlen("hello") is 5 but the array needs 6 bytes — the '\0' is counted in storage, not in length.sizeof(arr) gives the full byte size only where the array is declared; passed to a function it decays to a pointer and sizeof returns the pointer size.sizeof(arr)/sizeof(arr[0]) is the idiom to count elements — but only valid before decay.a[n] on an n-element array is out of bounds (undefined behavior, not a guaranteed crash).char *s = "hi"; points to read-only memory — modifying it is undefined; use char s[] = "hi"; for a mutable copy.strcpy/strcat do no bounds checking — a classic buffer-overflow vector; prefer strncpy/snprintf.== (compares addresses, not content) instead of strcmp."What does sizeof print inside vs outside a function?", off-by-one on the null terminator, "why is str1 == str2 wrong?", and 2D-array element-address arithmetic.
Arrays are contiguous; the name decays to a pointer except under sizeof/&. C-strings end in '\0', so "hello" is 6 bytes though strlen is 5. Use strcmp (not ==) to compare, and bounded functions to avoid overflow.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.