Coding/Learn/c/c arrays and multidimensional
C Foundational Course
01.Introduction, Compilation Pipeline & Syntax
02.Variables, Primitives & Explicit Type Casting
03.Operators, Precedence & Booleans (<stdbool.h>)
04.Control Flow: If-Else, Ternary & Switch
05.Loops (For, While, Do-While) & Jump Statements
06.Contiguous Arrays & Multi-Dimensional Matrices
07.Null-Terminated Strings & <string.h>
08.User Input, scanf & fgets Buffer Safety
09.Pointers, Memory Addresses & Dereferencing
10.Functions, Prototypes & Pass-by-Pointer
11.11. Recursion & High-Speed Function Pointers
12.File Streams: Create, Write & Read (fopen/fclose)
13.Structures, Unions & typedef Aliasing
14.Memory Word Alignment & Struct Padding
15.Enums & Bitwise Manipulation (Flags & Masks)
16.Dynamic Memory: malloc, calloc, realloc & free
17.Defensive Error Handling, errno & Assertions
18.Preprocessor Macros & Storage Classes
19.Fixed-Width Types (stdint.h), Math & Time (<time.h>)
20.Capstone: High-Performance In-Memory Key-Value Engine
Course Certificate0%

Complete all lessons to earn

LearnContiguous Arrays & Multi-Dimensional Matrices
6 / 20 0%
Lesson 06 of 20
6 mins read

Contiguous Arrays & Multi-Dimensional Matrices

PreviousNext Lesson
In Plain English

An array in C is a sequence of elements stored in unbroken contiguous memory addresses. Element access arr[i] computes instantly via base_address + (i * sizeof(type)). Calculate array length using sizeof(arr) / sizeof(arr[0]) at declaration scope.

The Mental Model

"An array is a row of contiguous lockers in a gym: once you know locker #0 is at address 1000 and each locker is 4 feet wide, locker #3 is guaranteed to be at address 1012."

Deep Dive: How It Works

Contiguous Storage: Elements are laid out side-by-side in linear memory without gaps.

Sizeof Calculation: sizeof(scores) / sizeof(scores[0]) computes total element count.

Row-Major 2D Matrices: 2D arrays are stored row-by-row in linear RAM.

Syntax Blueprint

int scores[4] = {88, 92, 79, 95};
size_t count = sizeof(scores) / sizeof(scores[0]);

Declare fixed-size array and calculate element count using compile-time sizeof.

PointCore Rules to Remember

Point star
Zero Overhead: No hidden length fields or object wrapper overhead.
Point star
No Bounds Checking: Accessing out-of-bounds indices corrupts adjacent memory silently.
Point star
Array Decay: Arrays decay into raw pointers when passed to functions.

Common Beginner Traps & How to Fix Them

Mistake:Using sizeof(arr) / sizeof(arr[0]) inside a function where arr was passed as a parameter.

Why it happens: In functions, arrays decay to pointers, so sizeof(arr) returns the pointer size (8 bytes), not the full array.

How to fix: Always pass the array length as an explicit size_t length parameter to functions.

Pro Tip: Always track array capacities explicitly to guard against buffer overruns.

Live Interactive Example

Hit Run Code to see it live
Average Score from Contiguous Array
C11 / GCC 9.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Output Console
Click "Run Code" to view the rendered output.
How it works: Loop sums all 4 scores (354) and divides by 4.0 yielding 88.5.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Calculate Array Size with sizeof

  • PointPrint "Total Elements: %zu\n" calculated using the sizeof idiom: sizeof(scores) / sizeof(scores[0]).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates