Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A one-dimensional array in C is a contiguous block of memory designed to store a fixed-size sequence of elements sharing a single data type. The array identifier represents the array object itself (of type T[N]). It is not a pointer or a reference; rather, during most expression evaluations, the identifier implicitly decays into a pointer to its first element.

Declaration Syntax

To declare a one-dimensional array, specify the data type, the array identifier, and the total number of elements enclosed in square brackets [].
data_type array_name[array_size];
  • data_type: Determines the amount of memory allocated per element and how the bit patterns are interpreted (e.g., int, char, float).
  • array_size: Must be an integer constant expression greater than zero (excluding C99 Variable Length Arrays). This dictates the total memory footprint: array_size * sizeof(data_type).

Initialization

Arrays can be initialized at the point of declaration using an initializer list enclosed in braces {}.
// 1. Full initialization
int arr1[5] = {10, 20, 30, 40, 50};

// 2. Implicit size derivation (compiler infers size as 4)
int arr2[] = {10, 20, 30, 40};

// 3. Partial initialization (remaining elements are zero-initialized)
int arr3[5] = {10, 20}; // Results in: {10, 20, 0, 0, 0}

// 4. Designated initializers (C99 standard)
int arr4[5] = {[1] = 10, [4] = 50}; // Results in: {0, 10, 0, 0, 50}

Memory Layout and Indexing

C arrays are zero-indexed. The index represents the offset from the base address. Because the memory is strictly contiguous, the compiler calculates the exact memory address of any element in constant time O(1)O(1) using pointer arithmetic: Address of array[i] = Base_Address + (i * sizeof(data_type))
int arr[3] = {10, 20, 30};

/* 
Assuming base address is 0x1000 and sizeof(int) is 4 bytes:
arr[0] -> 0x1000 (Value: 10)
arr[1] -> 0x1004 (Value: 20)
arr[2] -> 0x1008 (Value: 30)
*/

Access and Mutation

Elements are accessed and mutated using the subscript operator []. Under the hood, array[i] is syntactic sugar for dereferencing a pointer offset: *(array + i).
int arr[3] = {0};

// Mutation
arr[0] = 15;
arr[1] = 25;

// Access
int val = arr[1]; // val is now 25

Array-to-Pointer Decay

In most expressions, a one-dimensional array identifier “decays” into a pointer to its first element (&array[0]).
int arr[5];
int *ptr = arr; // arr decays to int*, equivalent to ptr = &arr[0]
Exceptions to Decay: The array does not decay to a pointer in three specific contexts:
  1. When it is the operand of the sizeof operator (returns the total byte size of the array object).
  2. When it is the operand of the & (address-of) operator (returns a pointer to the entire array, e.g., int (*)[5]).
  3. When it is a string literal used to initialize a character array.

Bounds Checking

The C standard dictates that there is no implicit bounds checking on array accesses.
int arr[5];
arr[10] = 100; // Compiles successfully, but causes Undefined Behavior (UB)
Writing to or reading from an index outside the range of 0 to array_size - 1 results in Undefined Behavior, which typically manifests as memory corruption, segmentation faults, or silent data mutation in adjacent memory blocks.
Master C with Deep Grasping Methodology!Learn More