GCGY Beta v1.0.1
GrowCodeEngineering
← Back to Hub
Back to Topics
c / c-orientation
18 mins

C Level 4: Contiguous Memory & Null Strings

Why This Matters: Arrays in C are raw contiguous memory blocks. Strings are character arrays terminated with '\0'.
## Array Indexing & String Buffers

An array allocates elements side-by-side in contiguous memory.

### C Null-Terminated Strings
Strings in C are character arrays that end with the null terminator character `'\0'`.

```c
char str[6] = {'H', 'e', 'l', 'l', 'o', ''};
// Or string literal equivalent:
char str[] = "Hello";
```
MENTAL MODEL & MEMORY LAYOUT
Memory Array Layout:
Index:     [0]   [1]   [2]   [3]   [4]   [5]
Value:    'H'   'e'   'l'   'l'   'o'   '\0'
Address: 0x10  0x11  0x12  0x13  0x14  0x15
COMMON PITFALLS TO AVOID
  • Forgetting extra byte for null terminator '\0' in string buffers.
  • Accessing array out of bounds (indexing arr[5] on size 5 array).
String Traversal in C
#include <stdio.h>

int main() {
    char name[] = "GrowCode";
    int len = 0;
    while (name[len] != '\0') {
        len++;
    }
    printf("String length: %d\n", len);
    return 0;
}

Iterates through character buffer until null terminator '\0' is reached.

CONCEPT MASTERY CHECKPOINT

Why is the null character `'\0'` mandatory at the end of C strings?

NEXT RECOMMENDED LESSON
C Level 5: Pointers & Addresses
Continue Path
Challenge: Calculate the sum of array elements `[1, 2, 3, 4, 5]` and print the sum.
C Level 4: Contiguous Memory & Null Strings
1
2
3
4
5
6
7
8
9
10
11
12
13
No test case execution results available yet. Click "Run Code" to evaluate your solution.