C Programming: Fundamentals of Two-Dimensional Arrays and Pointers

Two-Dimensional Arrays

Declaration

The syntax for declaring a two-dimensional array is: storage_class data_type array_name[rows][columns];

int matrix[2][3] = {1, 2, 3, 4, 5, 6}; // Declares a 2x3 integer matrix

Accessing Elements

Elements are accessed using array_name[row_index][column_index], where indices start from zero.

Note: Both row and column indices must be within the array bounds. When declaring, the number of rows can be omitted, but the number of columns cannot.

Size and Element Count

  • Element Count: The total number of elements is rows * columns. This can also be calculated as sizeof(array_name) / sizeof(data_type).
  • Total Size: The total memory size of the array is sizeof(data_type) * rows * columns, which is equivalent to sizeof(array_name).

Key Points:

  • array_name refers to the base address of the array.
  • array_name + 1 points to the beginning of the second row.
  • array_name[0] refers to the base address of the first row.
  • array_name[0][0] refers to the address of the first element in the first row.

Initialization

  1. Full Initialization:
    int matrix[2][3] = {1, 2, 3, 4, 5, 6}; // Sequential assignment
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; // Row-wise assignment
    
  2. Partial Initialization: Uninitialized elements are set to zero.
    int matrix[2][3] = {1, 2, 3, 4}; // Fills first 4 elements, rest are 0
    int matrix[2][3] = {{1, 3}, {4}}; // Fills {1, 3, 0}, {4, 0, 0}
    
  3. No Initialization: Elements will contain indeterminate values.
    int matrix[2][3]; // Contains garbage values
    matrix[0][0] = 1; // Individual element assignment
    

Memory Traversal

When traversing a two-dimensional array like matrix[2][4], incrementing the array name (matrix + 1) moves the pointer to the beginning of the next row, not just to the next element.

Pointers

Advantages

  • Enhance program concisaness, compactness, and efficiency.
  • Enable the representation of complex data structures.
  • Facilitate dynamic memory allocation for efficient space utilization.
  • Allow functions to return multiple values.

Core Concepts

  • Address: A unique identifier (number) for a memory location.
  • Pointer: A varible that stores a memory address.
  • Pointer Variable: A variable specifically designed to hold memory addresses.

Declaration

The syntax for declaring a pointer variable is: storage_class data_type *variable_name;

int *ptr; // Declares a pointer variable named ptr

Note: The data type of a pointer indicates the type of data it is expected to point to.

Pointer and Variable Relationship

  • *ptr accesses the value stored at the address ptr points to.
  • ptr holds the memory address.
int variable = 10;
int *ptr = &variable;

// *ptr will have the value 10
// ptr will hold the address of 'variable'

Pointer Operators

  • &: Address-of operator (retrieves the memory address of a variable).
  • *: Dereference operator (accesses the value at the memory address).

Examples:

  • &ptr: Address of the pointer variable ptr itself.
  • *&variable: Equivalent to variable.
  • &*variable: Incorrect usage.

Initialization

  1. Pointing to a regular variable:
    int value = 0;
    int *pointer = &value;
    
  2. Pointing to an array: A pointer to the first element of an array.
    int arr[5] = {10, 20, 30, 40, 50};
    int *pointer = arr; // Equivalent to int *pointer = &arr[0];
    
  3. Assigning one pointer to another: Copying the address held by one pointer to another.
    int a = 0;
    int *p1 = &a;
    int *p2 = p1;
    

Pointer Arithmetic

Pointer arithmetic calculates new addresses based on the data type the pointer points to. Incrementing/decrementing a pointer moves it by multiples of the size of its base data type.

  • pointer + n: Moves the pointer n elements forward in memory (towards higher addresses). The pointer's own address does not change, but its target address does.
  • pointer - n: Moves the pointer n elements backward in memory (towards lower addresses).
  • pointer++: Increments the pointer to point to the next element of its data type.
  • pointer--: Decrements the pointer to point to the previous element of its data type.

Example:

  • int *ptr; ptr++; // Moves the pointer forward by sizeof(int) bytes (typically 4).
  • char *ptr; ptr++; // Moves the pointer forward by sizeof(char) byte (typically 1).

Tags: C Arrays pointers Memory Management Data Structures

Posted on Mon, 01 Jun 2026 16:28:54 +0000 by tllewellyn