CodeWithAbdessamad

Arrays

Arrays

๐Ÿ“Š

Single-dimensional Arrays

A single-dimensional array is a contiguous block of memory that stores elements of the same type in a linear sequence. Itโ€™s the simplest form of array in C and serves as the foundation for more complex data structures. Each element is accessed using a single index starting from 0.

Declaration and Initialization

Single-dimensional arrays are declared with a specified size and can be initialized in multiple ways:

<code class="language-c">int numbers[3] = {1, 2, 3}; // Explicit size
<p>int numbers[] = {1, 2, 3};   // Implicit size (determined by initializers)</p>
<p>int numbers[3] = {1};        // Partial initialization (remaining elements = 0)</code>

Key Characteristics

  • Indexing: Elements are accessed via array[index] (e.g., numbers[0] = 1)
  • Bounds Safety: Accessing beyond the defined size causes buffer overflow (a critical security risk)
  • Iteration: Use loops to traverse elements efficiently

Practical Example

This program demonstrates array iteration and bounds safety:

<code class="language-c">#include <stdio.h>

<p>int main() {</p>
<p>    int numbers[3] = {1, 2, 3};</p>
<p>    for (int i = 0; i < 3; i++) {</p>
<p>        printf("numbers[%d] = %d\n", i, numbers[i]);</p>
<p>    }</p>
<p>    // Safe access: numbers[2] (last index)</p>
<p>    printf("numbers[3] = %d\n", numbers[3]); // Buffer overflow! (uninitialized)</p>
<p>    return 0;</p>
<p>}</code>

Initialization Comparison Table

Method Example Size Notes
Explicit size int arr[5] = {1, 2} 5 Elements 2-4 = 0
Implicit size int arr[] = {1, 2} 2 Size auto-determined by initializers
Partial initialization int arr[5] = {1} 5 Elements 1-4 = 0

Multi-dimensional Arrays

Multi-dimensional arrays extend single-dimensional arrays to represent data in multiple directions (e.g., 2D matrices, 3D cubes). C treats them as arrays of arrays, with the innermost dimension being the most basic unit.

Two-dimensional Arrays

A 2D array is a matrix with rows ร— columns. Elements are accessed using two indices: [row][column].

Declaration and Initialization

<code class="language-c">int matrix[3][4] = {
<p>    {1, 2, 3, 4},  // Row 0</p>
<p>    {5, 6, 7, 8},  // Row 1</p>
<p>    {9, 10, 11, 12} // Row 2</p>
<p>};</code>

Practical Example

This program iterates through a 3×4 matrix:

<code class="language-c">#include <stdio.h>

<p>int main() {</p>
<p>    int matrix[3][4] = {</p>
<p>        {1, 2, 3, 4},</p>
<p>        {5, 6, 7, 8},</p>
<p>        {9, 10, 11, 12}</p>
<p>    };</p>
<p>    </p>
<p>    for (int i = 0; i < 3; i++) {</p>
<p>        for (int j = 0; j < 4; j++) {</p>
<p>            printf("matrix[%d][%d] = %d\n", i, j, matrix[i][j]);</p>
<p>        }</p>
<p>    }</p>
<p>    return 0;</p>
<p>}</code>

Three-dimensional Arrays

A 3D array is an array of 2D arrays (e.g., layers ร— rows ร— columns). Elements are accessed via [layer][row][column].

Declaration and Initialization

<code class="language-c">int cube[2][3][4] = {
<p>    {</p>
<p>        {1, 2, 3, 4},  // Layer 0, Row 0</p>
<p>        {5, 6, 7, 8},  // Layer 0, Row 1</p>
<p>        {9, 10, 11, 12} // Layer 0, Row 2</p>
<p>    },</p>
<p>    {</p>
<p>        {13, 14, 15, 16}, // Layer 1, Row 0</p>
<p>        {17, 18, 19, 20}, // Layer 1, Row 1</p>
<p>        {21, 22, 23, 24}  // Layer 1, Row 2</p>
<p>    }</p>
<p>};</code>

Practical Example

Accessing a specific element in a 3D array:

<code class="language-c">#include <stdio.h>

<p>int main() {</p>
<p>    int cube[2][3][4] = {</p>
<p>        {</p>
<p>            {1, 2, 3, 4},</p>
<p>            {5, 6, 7, 8},</p>
<p>            {9, 10, 11, 12}</p>
<p>        },</p>
<p>        {</p>
<p>            {13, 14, 15, 16},</p>
<p>            {17, 18, 19, 20},</p>
<p>            {21, 22, 23, 24}</p>
<p>        }</p>
<p>    };</p>
<p>    </p>
<p>    printf("cube[0][1][2] = %d\n", cube[0][1][2]); // Output: 11</p>
<p>    return 0;</p>
<p>}</code>

Critical Notes

  1. Memory Layout: C stores multi-dimensional arrays in row-major order (all elements of the first dimension are contiguous).
  2. Bounds Safety: Always verify indices to prevent buffer overflows (e.g., matrix[3][0] for a 3-row array).
  3. Use Cases: Essential for graphics, scientific computing, and grid-based systems.

Summary

Single-dimensional arrays provide the fundamental building block for linear data storage in C, while multi-dimensional arrays (2D and 3D) enable efficient representation of structured data like matrices and 3D grids. Critical best practices include strict index validation to avoid buffer overflowsโ€”the most common cause of security vulnerabilities and program crashes. Always prioritize bounds checking when working with arrays to ensure robust and safe applications.

๐ŸŒŸ