c2cedge
C, Java & Python · B — C Essentials

C: Arrays, Strings & Functions

In C, arrays and pointers are deeply linked, strings are just char arrays with a terminator, and functions copy their arguments. Each fact spawns classic questions.

Test weight: High (C)Skill: Arrays as pointersDifficulty: Medium

C arrays, strings and functions are tied together by one idea: an array name decays to a pointer to its first element. A C string is simply a char array ending in a null terminator '\0'. And because C passes by value, passing an array actually passes that decayed pointer — which has real consequences for things like sizeof.

Arrays decay to pointers; strings are null-terminated

An array name used in most expressions becomes the address of its first element. A string literal "hi" is stored as {'h','i','\0'} — the '\0' marks the end, which is how functions like strlen know where to stop.

Strings are char arrays
char s[] = "hi";        // actually {'h','i','\0'} — 3 bytes
printf("%lu", strlen(s)); // 2  (counts up to, not including, '\0')
printf("%lu", sizeof(s)); // 3  (includes the terminator)
sizeof inside a function differs
void f(int a[]) {
    // here 'a' is really a pointer, so sizeof(a) is the POINTER size (e.g. 8)
    printf("%lu", sizeof(a));
}
int main(void) {
    int arr[10];
    printf("%lu", sizeof(arr)); // 40 = 10 * 4  (full array, in main)
    f(arr);
}
⚡ The edge
  • An array name is the address of its first element, so arr[i] is exactly *(arr + i). When you pass an array to a function, you pass that pointer — the function can't tell the original length.
  • sizeof on an array gives the whole array's bytes; on a pointer it gives the pointer's size. That's why sizeof inside a function (where the parameter is a pointer) differs from sizeof on the real array — a favourite trick question.
Worked example
Why is sizeof different on an array in main vs inside a function?
  1. In main, arr is the actual array, so sizeof(arr) is the total bytes (elements × element size).
  2. When passed to a function, the array decays to a pointer to its first element.
  3. Inside the function the parameter is a pointer, so sizeof gives the pointer's size, not the array's — you must pass the length separately.
Worked example
How does strlen know where a string ends?
  1. C strings are terminated by a null character '\0'.
  2. strlen walks from the start, counting characters until it hits '\0'.
  3. It returns that count — not including the terminator. A missing '\0' makes strlen read past the buffer (a bug).
⚠ Watch out
  • Off-by-one with the null terminator: a string of n characters needs n+1 bytes.
  • C does no bounds checking — writing past an array's end is undefined behaviour (buffer overflow).
  • sizeof vs strlen: sizeof gives the buffer's bytes; strlen counts characters up to '\0'.
Practice this — take a timed mock →
1,300+ questions, scored, with a weak-area report.
Know who's ready. Not who finished.
HomeLibraryPrivacyTerms