Skip to content

C Arrays

Arrays are collections of elements of the same type stored in contiguous memory locations.

1. Array Declaration and Initialization

c
int arr[5];                    // declaration
int arr2[] = {1, 2, 3, 4, 5}; // initialization
int arr3[5] = {1, 2, 3};       // partial initialization

2. Array Access

c
arr[0] = 10;  // first element
arr[4] = 50;  // last element

3. Multidimensional Arrays

c
int matrix[3][4];  // 3x4 matrix
int matrix2[][2] = {{1, 2}, {3, 4}, {5, 6}};  // 3x2 matrix

4. Arrays and Pointers

c
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;  // array name decays to pointer

5. Common Operations

c
int sum = 0;
for (int i = 0; i < 5; i++) {
    sum += arr[i];
}

Content is for learning and research only.