Arrays in C can be categorized based on how many dimensions they have and how they’re used.
1️⃣ One-Dimensional Array (1D Array)
A 1D array is like a row of boxes where each box stores a value.
🎯 Analogy: A row of lockers 📦📦📦
✅ Declaration:
int marks[5];This creates 5 integer slots: marks[0], marks[1], ..., marks[4]
✅ Initialization:
int marks[5] = {85, 90, 75, 60, 95};This stores values in a linear fashion in memory.
✅ Accessing Elements:
printf("%d", marks[2]); // Prints 75Indexing always starts from 0
✅ Use Case:
Used when you need to store:
List of scores 🏅
Product prices 💰
Temperatures over a week 🌡️
🔹 2️⃣ Two-Dimensional Array (2D Array)
A 2D array is like a grid or a matrix — rows and columns.
🎯 Analogy: A chessboard or spreadsheet
✅ Declaration:
int matrix[3][2];This creates:
3 rows
2 columns in each row
So it can hold 3 × 2 = 6 elements
✅ Initialization:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};matrix[0][0] | matrix[0][1] | matrix[0][2] |
|---|---|---|
1 | 2 | 3 |
matrix[1][0] | matrix[1][1] | matrix[1][2] |
|---|---|---|
4 | 5 | 6 |
✅ Accessing Elements:
printf("%d", matrix[1][2]); // Output: 6✅ Use Case:
Used for:
Storing data in rows and columns
Matrix operations
Tables or scoreboards
🔹 3️⃣ Three-Dimensional Array (3D Array)
A 3D array is like a cube made of multiple matrices.
🎯 Analogy: A Rubik’s Cube, or a set of spreadsheets stacked on top of each other 📊📊📊
✅ Declaration:
int cube[2][3][4];2 blocks (pages)
Each block has 3 rows
Each row has 4 columns
→ Total: 2 × 3 × 4 = 24 elements
✅ Initialization:
int cube[1][2][2] = {
{
{1, 2},
{3, 4}
}
};Visualize as:
1 layer → 2 rows → 2 columns each
Accessing:
printf("%d", cube[0][1][1]); // Output: 4✅ Use Case:
Used in:
Scientific calculations
3D graphics
Complex data tables (like time-series of 2D data)
<div class="infographic-carray">
<style>
.infographic-carray {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--code-bg: #1e1e2e;
--code-text: #d4d4e0;
--syn-keyword: #c586c0;
--syn-func: #dcdcaa;
--syn-comment: #6a9955;
--warn: #b45309;
--warn-soft: #fdf0dc;
}
.infographic-carray[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--code-bg: #0d0d14;
--code-text: #d4d4e0;
--warn: #e0a458;
--warn-soft: #3a2c14;
}
.infographic-carray {
box-sizing: border-box;
max-width: 760px;
margin: 0 auto;
background: var(--paper);
color: var(--ink);
font-family: 'Inter', -apple-system, sans-serif;
padding: 0 clamp(1rem, 4vw, 2.25rem) 1.25rem;
}
.infographic-carray *, .infographic-carray *::before, .infographic-carray *::after {
box-sizing: border-box;
}
.infographic-carray h1 {
font-family: 'Fraunces', Georgia, serif;
font-weight: 600;
font-size: clamp(1.4rem, 3.5vw, 1.9rem);
margin: 1.25rem 0 0.25rem;
}
.infographic-carray .subhead { color: var(--ink-soft); font-size: 0.85rem; margin-bottom: 1rem; }
.infographic-carray .chip-row { display: flex; flex-wrap: wrap; gap: 0.45rem; margin-bottom: 1rem; }
.infographic-carray .chip {
all: unset;
cursor: pointer;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.82rem;
font-weight: 600;
padding: 0.42rem 0.9rem;
border-radius: 20px;
border: 1px solid var(--line);
color: var(--ink-soft);
transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.infographic-carray .chip:hover { border-color: var(--accent); }
.infographic-carray .chip.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.infographic-carray .code-panel {
background: var(--code-bg);
color: var(--code-text);
border-radius: 8px;
padding: 0.7rem 0.9rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.8rem;
line-height: 1.6;
margin-bottom: 0.9rem;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
max-width: 100%;
}
.infographic-carray .code-panel .code-line { white-space: pre; display: block; }
.infographic-carray .syn-keyword { color: var(--syn-keyword); }
.infographic-carray .syn-func { color: var(--syn-func); }
.infographic-carray .syn-comment { color: var(--syn-comment); }
.infographic-carray .field-row { display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; margin-bottom: 0.9rem; }
.infographic-carray .field-row label { font-size: 0.82rem; color: var(--ink-soft); }
.infographic-carray .field-row input {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.88rem;
padding: 0.32rem 0.5rem;
border-radius: 6px;
border: 1px solid var(--line);
background: var(--paper);
color: var(--ink);
}
.infographic-carray input[type="text"] { width: 15rem; max-width: 100%; }
.infographic-carray input[type="number"] { width: 3.6rem; }
.infographic-carray .field-row input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.infographic-carray .grid-2d { display: flex; flex-direction: column; gap: 0.4rem; margin-bottom: 0.5rem; }
.infographic-carray .array-row { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.infographic-carray .cell {
cursor: pointer;
min-width: 3rem;
text-align: center;
border: 1px solid var(--line);
border-radius: 8px;
padding: 0.45rem 0.25rem 0.35rem;
background: var(--paper-alt);
transition: border-color 0.2s, background 0.2s, transform 0.15s;
}
.infographic-carray .cell:hover { transform: translateY(-2px); }
.infographic-carray .cell .idx {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.63rem;
color: var(--ink-soft);
margin-bottom: 0.1rem;
}
.infographic-carray .cell .val {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.95rem;
font-weight: 700;
color: var(--ink);
}
.infographic-carray .cell.is-selected {
border-color: var(--accent);
background: var(--accent-soft);
box-shadow: 0 3px 10px -4px rgba(0,0,0,0.25);
}
.infographic-carray .cell.is-selected .idx,
.infographic-carray .cell.is-selected .val { color: var(--accent); }
.infographic-carray .facts { font-size: 0.78rem; color: var(--ink-soft); margin-bottom: 1rem; }
.infographic-carray .result-panel {
padding: 0.75rem 1rem;
background: var(--paper-alt);
border-radius: 8px;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.85rem;
color: var(--ink);
min-height: 2.4rem;
}
.infographic-carray .result-panel.is-warn {
background: var(--warn-soft);
color: var(--warn);
font-family: 'Inter', sans-serif;
font-weight: 600;
}
.infographic-carray .mode-panel { display: none; }
.infographic-carray .mode-panel.is-active { display: block; }
.infographic-carray .takeaway { font-size: 0.82rem; color: var(--ink-soft); margin-top: 1.25rem; border-top: 1px solid var(--line); padding-top: 0.9rem; }
.infographic-carray .takeaway strong { color: var(--ink); }
@media (prefers-reduced-motion: reduce) {
.infographic-carray * { transition: none !important; }
}
</style>
<h1>🧱 Arrays in C</h1>
<p class="subhead">1D, 2D, and 3D — same idea stacked deeper. Resize, click a box, or look up any index.</p>
<div class="chip-row" id="dim-tabs">
<button class="chip is-active" data-dim="1">1D</button>
<button class="chip" data-dim="2">2D</button>
<button class="chip" data-dim="3">3D</button>
</div>
<!-- 1D -->
<div class="mode-panel is-active" data-dim="1">
<div class="code-panel"><span class="code-line" id="d1-code"></span></div>
<div class="field-row">
<label>Array values:</label>
<input type="text" id="d1-values" value="10, 20, 30, 40, 50">
</div>
<div class="array-row" id="d1-cells"></div>
<p class="facts" id="d1-facts"></p>
<div class="field-row">
<label>arr[ <input type="number" id="d1-index" value="0"> ]</label>
</div>
<div class="result-panel" id="d1-result"></div>
</div>
<!-- 2D -->
<div class="mode-panel" data-dim="2">
<div class="code-panel"><span class="code-line" id="d2-code"></span></div>
<div class="field-row">
<label>rows <input type="number" id="d2-rows" value="3" min="1" max="6"></label>
<label>cols <input type="number" id="d2-cols" value="3" min="1" max="6"></label>
</div>
<div class="grid-2d" id="d2-grid"></div>
<p class="facts" id="d2-facts"></p>
<div class="field-row">
<label>arr[ <input type="number" id="d2-row" value="0"> ][ <input type="number" id="d2-col" value="0"> ]</label>
</div>
<div class="result-panel" id="d2-result"></div>
</div>
<!-- 3D -->
<div class="mode-panel" data-dim="3">
<div class="code-panel"><span class="code-line" id="d3-code"></span></div>
<div class="field-row">
<label>depth <input type="number" id="d3-depth" value="2" min="1" max="4"></label>
<label>rows <input type="number" id="d3-rows" value="2" min="1" max="5"></label>
<label>cols <input type="number" id="d3-cols" value="2" min="1" max="5"></label>
</div>
<div class="chip-row" id="d3-layers"></div>
<div class="grid-2d" id="d3-grid"></div>
<p class="facts" id="d3-facts"></p>
<div class="field-row">
<label>arr[ <input type="number" id="d3-depthidx" value="0"> ][ <input type="number" id="d3-row" value="0"> ][ <input type="number" id="d3-col" value="0"> ]</label>
</div>
<div class="result-panel" id="d3-result"></div>
</div>
<p class="takeaway"><strong>Same rule at every depth:</strong> every dimension you add is really just an array of arrays — and C still lays it all out as one flat, contiguous block in memory.</p>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-carray')
|| document.querySelector('.infographic-carray');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') { dark = true; }
else if (bodyAttr === 'false') { dark = false; }
else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
function $(sel) { return wrapper.querySelector(sel); }
const BASE_ADDR = 0x7ffe1000;
// ---- tab switching ----
const dimTabs = wrapper.querySelectorAll('#dim-tabs .chip');
const panels = wrapper.querySelectorAll('.mode-panel');
dimTabs.forEach(tab => {
tab.addEventListener('click', () => {
dimTabs.forEach(t => t.classList.remove('is-active'));
tab.classList.add('is-active');
panels.forEach(p => p.classList.toggle('is-active', p.dataset.dim === tab.dataset.dim));
});
});
// ================= 1D =================
const d1ValuesInput = $('#d1-values');
const d1IndexInput = $('#d1-index');
const d1Cells = $('#d1-cells');
const d1Code = $('#d1-code');
const d1Facts = $('#d1-facts');
const d1Result = $('#d1-result');
let arr1 = [];
function d1Parse() {
arr1 = d1ValuesInput.value.split(',').map(s => s.trim()).filter(s => s.length)
.map(s => { const n = parseInt(s, 10); return isNaN(n) ? 0 : n; });
if (arr1.length === 0) arr1 = [0];
}
function d1Render() {
d1Code.innerHTML = '<span class="syn-keyword">int</span> <span class="syn-func">arr</span>[' + arr1.length + '] = {' + arr1.join(', ') + '};';
d1Cells.innerHTML = '';
arr1.forEach((v, i) => {
const cell = document.createElement('div');
cell.className = 'cell';
cell.innerHTML = '<div class="idx">' + i + '</div><div class="val">' + v + '</div>';
cell.addEventListener('click', () => { d1IndexInput.value = i; d1Lookup(); });
d1Cells.appendChild(cell);
});
d1Facts.textContent = 'arr has ' + arr1.length + ' elements — valid indices are 0 to ' + (arr1.length - 1) +
'. sizeof(arr) = ' + (arr1.length * 4) + ' bytes.';
}
function d1Lookup() {
const i = parseInt(d1IndexInput.value, 10);
d1Cells.querySelectorAll('.cell').forEach((c, ci) => c.classList.toggle('is-selected', ci === i));
if (isNaN(i)) { d1Result.className = 'result-panel'; d1Result.textContent = ''; return; }
if (i >= 0 && i < arr1.length) {
const addr = (BASE_ADDR + i * 4).toString(16);
d1Result.className = 'result-panel';
d1Result.textContent = 'arr[' + i + '] = ' + arr1[i] + ' (address 0x' + addr + ')';
} else {
d1Result.className = 'result-panel is-warn';
d1Result.textContent = '⚠️ arr[' + i + '] is out of bounds — undefined behavior.';
}
}
function d1RebuildAll() { d1Parse(); d1Render(); d1Lookup(); }
d1ValuesInput.addEventListener('input', d1RebuildAll);
d1IndexInput.addEventListener('input', d1Lookup);
// ================= 2D =================
const d2RowsInput = $('#d2-rows');
const d2ColsInput = $('#d2-cols');
const d2Grid = $('#d2-grid');
const d2Code = $('#d2-code');
const d2Facts = $('#d2-facts');
const d2RowIdx = $('#d2-row');
const d2ColIdx = $('#d2-col');
const d2Result = $('#d2-result');
let rows2 = 3, cols2 = 3;
function d2Val(r, c) { return r * cols2 + c; }
function d2Render() {
rows2 = Math.max(1, Math.min(6, parseInt(d2RowsInput.value, 10) || 1));
cols2 = Math.max(1, Math.min(6, parseInt(d2ColsInput.value, 10) || 1));
d2RowsInput.value = rows2; d2ColsInput.value = cols2;
const rowsText = [];
for (let r = 0; r < rows2; r++) {
const vals = [];
for (let c = 0; c < cols2; c++) vals.push(d2Val(r, c));
rowsText.push('{' + vals.join(', ') + '}');
}
d2Code.innerHTML = '<span class="syn-keyword">int</span> <span class="syn-func">arr</span>[' + rows2 + '][' + cols2 + '] = {\n ' + rowsText.join(',\n ') + '\n};';
d2Grid.innerHTML = '';
for (let r = 0; r < rows2; r++) {
const rowEl = document.createElement('div');
rowEl.className = 'array-row';
for (let c = 0; c < cols2; c++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.r = r; cell.dataset.c = c;
cell.innerHTML = '<div class="idx">' + r + ',' + c + '</div><div class="val">' + d2Val(r, c) + '</div>';
cell.addEventListener('click', () => { d2RowIdx.value = r; d2ColIdx.value = c; d2Lookup(); });
rowEl.appendChild(cell);
}
d2Grid.appendChild(rowEl);
}
d2Facts.textContent = 'arr is ' + rows2 + ' × ' + cols2 + ' = ' + (rows2 * cols2) + ' elements. ' +
'sizeof(arr) = ' + (rows2 * cols2 * 4) + ' bytes. Stored row by row (row-major order).';
}
function d2Lookup() {
const r = parseInt(d2RowIdx.value, 10);
const c = parseInt(d2ColIdx.value, 10);
d2Grid.querySelectorAll('.cell').forEach(cell => {
cell.classList.toggle('is-selected', +cell.dataset.r === r && +cell.dataset.c === c);
});
if (isNaN(r) || isNaN(c)) { d2Result.className = 'result-panel'; d2Result.textContent = ''; return; }
if (r >= 0 && r < rows2 && c >= 0 && c < cols2) {
const flat = r * cols2 + c;
const addr = (BASE_ADDR + flat * 4).toString(16);
d2Result.className = 'result-panel';
d2Result.textContent = 'arr[' + r + '][' + c + '] = ' + d2Val(r, c) + ' (flat index ' + flat + ', address 0x' + addr + ')';
} else {
d2Result.className = 'result-panel is-warn';
d2Result.textContent = '⚠️ arr[' + r + '][' + c + '] is out of bounds — undefined behavior.';
}
}
d2RowsInput.addEventListener('input', () => { d2Render(); d2Lookup(); });
d2ColsInput.addEventListener('input', () => { d2Render(); d2Lookup(); });
d2RowIdx.addEventListener('input', d2Lookup);
d2ColIdx.addEventListener('input', d2Lookup);
// ================= 3D =================
const d3DepthInput = $('#d3-depth');
const d3RowsInput = $('#d3-rows');
const d3ColsInput = $('#d3-cols');
const d3Layers = $('#d3-layers');
const d3Grid = $('#d3-grid');
const d3Code = $('#d3-code');
const d3Facts = $('#d3-facts');
const d3DepthIdx = $('#d3-depthidx');
const d3RowIdx = $('#d3-row');
const d3ColIdx = $('#d3-col');
const d3Result = $('#d3-result');
let depth3 = 2, rows3 = 2, cols3 = 2, activeLayer = 0;
function d3Val(d, r, c) { return d * rows3 * cols3 + r * cols3 + c; }
function d3RenderLayers() {
d3Layers.innerHTML = '';
for (let d = 0; d < depth3; d++) {
const btn = document.createElement('button');
btn.className = 'chip' + (d === activeLayer ? ' is-active' : '');
btn.textContent = 'layer ' + d;
btn.addEventListener('click', () => {
activeLayer = d;
d3RenderLayers();
d3RenderGrid();
d3Lookup();
});
d3Layers.appendChild(btn);
}
}
function d3RenderGrid() {
d3Grid.innerHTML = '';
for (let r = 0; r < rows3; r++) {
const rowEl = document.createElement('div');
rowEl.className = 'array-row';
for (let c = 0; c < cols3; c++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.d = activeLayer; cell.dataset.r = r; cell.dataset.c = c;
cell.innerHTML = '<div class="idx">' + activeLayer + ',' + r + ',' + c + '</div><div class="val">' + d3Val(activeLayer, r, c) + '</div>';
cell.addEventListener('click', () => {
d3DepthIdx.value = activeLayer; d3RowIdx.value = r; d3ColIdx.value = c; d3Lookup();
});
rowEl.appendChild(cell);
}
d3Grid.appendChild(rowEl);
}
}
function d3Render() {
depth3 = Math.max(1, Math.min(4, parseInt(d3DepthInput.value, 10) || 1));
rows3 = Math.max(1, Math.min(5, parseInt(d3RowsInput.value, 10) || 1));
cols3 = Math.max(1, Math.min(5, parseInt(d3ColsInput.value, 10) || 1));
d3DepthInput.value = depth3; d3RowsInput.value = rows3; d3ColsInput.value = cols3;
if (activeLayer >= depth3) activeLayer = 0;
d3Code.innerHTML = '<span class="syn-keyword">int</span> <span class="syn-func">arr</span>[' + depth3 + '][' + rows3 + '][' + cols3 + '];';
d3Facts.textContent = 'arr is ' + depth3 + ' × ' + rows3 + ' × ' + cols3 + ' = ' + (depth3 * rows3 * cols3) + ' elements. ' +
'sizeof(arr) = ' + (depth3 * rows3 * cols3 * 4) + ' bytes. Viewing one 2D layer at a time below.';
d3RenderLayers();
d3RenderGrid();
d3Lookup();
}
function d3Lookup() {
const d = parseInt(d3DepthIdx.value, 10);
const r = parseInt(d3RowIdx.value, 10);
const c = parseInt(d3ColIdx.value, 10);
d3Grid.querySelectorAll('.cell').forEach(cell => {
cell.classList.toggle('is-selected', +cell.dataset.d === d && +cell.dataset.r === r && +cell.dataset.c === c);
});
if (isNaN(d) || isNaN(r) || isNaN(c)) { d3Result.className = 'result-panel'; d3Result.textContent = ''; return; }
if (d >= 0 && d < depth3 && r >= 0 && r < rows3 && c >= 0 && c < cols3) {
const flat = d * rows3 * cols3 + r * cols3 + c;
const addr = (BASE_ADDR + flat * 4).toString(16);
d3Result.className = 'result-panel';
d3Result.textContent = 'arr[' + d + '][' + r + '][' + c + '] = ' + d3Val(d, r, c) + ' (flat index ' + flat + ', address 0x' + addr + ')';
} else {
d3Result.className = 'result-panel is-warn';
d3Result.textContent = '⚠️ arr[' + d + '][' + r + '][' + c + '] is out of bounds — undefined behavior.';
}
}
d3DepthInput.addEventListener('input', d3Render);
d3RowsInput.addEventListener('input', d3Render);
d3ColsInput.addEventListener('input', d3Render);
d3DepthIdx.addEventListener('input', () => {
const d = parseInt(d3DepthIdx.value, 10);
if (!isNaN(d) && d >= 0 && d < depth3 && d !== activeLayer) {
activeLayer = d;
d3RenderLayers();
d3RenderGrid();
}
d3Lookup();
});
d3RowIdx.addEventListener('input', d3Lookup);
d3ColIdx.addEventListener('input', d3Lookup);
// ---- init ----
d1RebuildAll();
d2Render();
d2Lookup();
d3Render();
})();
</script>🆚 Comparison Table
Type | Dimensions | Syntax | Visual |
|---|---|---|---|
1D Array | 1 |
| 🧱🧱🧱🧱🧱 |
2D Array | 2 |
| 📊 Grid |
3D Array | 3 |
| 🧊 Cube |
🔥 Bonus: Higher-Dimensional Arrays
C technically supports more than 3 dimensions (like 4D, 5D), but they are:
Difficult to manage
Rarely used in practice
Mainly useful in scientific or simulation-heavy applications
⚠️ Key Things to Remember
You must declare all dimensions' sizes, except for the first during initialization.
int arr[][3] = {
{1, 2, 3},
{4, 5, 6}
}; // ✅ OKBut this is not allowed:
int arr[][] = {{1,2}, {3,4}}; // ❌ Error🧠 Memory Layout Insight
C stores multi-dimensional arrays in row-major order:
For int arr[2][3] = {{1,2,3}, {4,5,6}};
Memory layout is:
Index: [0][0] [0][1] [0][2] [1][0] [1][1] [1][2]
Value: 1 2 3 4 5 6