C Programs Tutorials | IT Developer
IT Developer

C Programming - C Arrays



Share with a Friend

C Programming - C Arrays

C Arrays

An array in C is a collection of elements of the same data type, stored in contiguous memory locations. It allows you to store multiple values in a single variable, which can be accessed by using an index or subscript. Arrays are essential for handling large sets of data efficiently and are widely used in various applications.

Key Characteristics of C Arrays:

  1. Homogeneous Data Type: All elements in an array must be of the same data type (e.g., all integers, all floats).
  2. Fixed Size: The size of an array is fixed when it is declared, meaning you cannot change its size after initialization.
  3. Indexed Access: Array elements are accessed using an index or subscript, with indexing starting from 0.
  4. Contiguous Memory Allocation: Array elements are stored in consecutive memory locations, which allows for efficient access.

Syntax for Declaring Arrays in C:

C

data_type array_name[array_size];

Where:

  • data_type is the type of data to be stored in the array (e.g., int, float, char).
  • array_name is the name of the array.
  • array_size is the number of elements the array will hold.

Example: Basic Array Declaration

C

#include <stdio.h>

 

int main() {

    int arr[5];  // Declare an integer array of size 5

   

    // Initializing the array

    arr[0] = 10;

    arr[1] = 20;

    arr[2] = 30;

    arr[3] = 40;

    arr[4] = 50;

 

    // Accessing array elements and displaying them

    for (int i = 0; i < 5; i++) {

        printf("arr[%d] = %d\n", i, arr[i]);

    }

 

    return 0;

}

Explanation:

  • An integer array arr of size 5 is declared.
  • The array is manually initialized with values 10, 20, 30, 40, 50.
  • The for loop is used to print all the elements in the array using their indices.

Array Initialization

Arrays can be initialized at the time of declaration, either with specific values or automatically.

  1. Explicit Initialization:

C

int arr[5] = {1, 2, 3, 4, 5};  // Initialize with specific values

  1. Partial Initialization:

If you specify fewer values than the array size, the remaining elements are automatically initialized to 0 (for numeric types).

C

int arr[5] = {1, 2};  // Initializes arr[0] = 1, arr[1] = 2, arr[2..4] = 0

  1. Implicit Size:

If the size of the array is not specified, the compiler will automatically determine it based on the number of elements in the initializer list.

C

int arr[] = {1, 2, 3, 4, 5};  // The size of the array will be 5

Accessing Array Elements

Array elements are accessed using their index, starting from 0 to n-1, where n is the size of the array.

C

int x = arr[2];  // Accessing the 3rd element of the array (index 2)

Multidimensional Arrays

C allows arrays with more than one dimension, such as 2D arrays (matrices), which can be represented as an array of arrays.

  1. 2D Array Declaration:

C

data_type array_name[rows][columns];

  1. Example of a 2D Array:

C

#include <stdio.h>

 

int main() {

    int arr[3][3] = {

        {1, 2, 3},

        {4, 5, 6},

        {7, 8, 9}

    };

 

    // Printing elements of the 2D array

    for (int i = 0; i < 3; i++) {

        for (int j = 0; j < 3; j++) {

            printf("arr[%d][%d] = %d\n", i, j, arr[i][j]);

        }

    }

 

    return 0;

}

Explanation:

  • A 2D array arr of size 3x3 is declared and initialized.
  • A nested for loop is used to iterate through the rows and columns of the array.

Arrays and Pointers

In C, arrays and pointers are closely related. The name of an array can be thought of as a pointer to the first element of the array. Therefore, you can use pointer arithmetic to access array elements.

  1. Array Name as a Pointer:

C

int arr[] = {1, 2, 3};

int *ptr = arr;  // Pointer to the first element of the array

  1. Accessing Array Elements via Pointer:

C

printf("%d\n", *(ptr + 1));  // Accessing the second element of the array

Array Passing to Functions

In C, arrays are passed to functions by reference, meaning that the function receives the address of the first element of the array, and any changes made to the array inside the function affect the original array.

  1. Passing Array to a Function:

C

#include <stdio.h>

 

void modifyArray(int arr[]) {

    arr[0] = 100;  // Modify the first element of the array

}

 

int main() {

    int arr[] = {1, 2, 3, 4, 5};

    modifyArray(arr);

    printf("arr[0] = %d\n", arr[0]);  // Output: arr[0] = 100

    return 0;

}

Explanation:

  • The array arr is passed to the modifyArray function by reference, allowing it to modify the original array.

Limitations of Arrays in C

  1. Fixed Size: The size of an array must be specified at compile time, and cannot be changed dynamically during runtime (unless using dynamic memory allocation).
  2. Lack of Bound Checking: C does not automatically check if an array index is out of bounds, so accessing an invalid index can lead to undefined behavior.
  3. Homogeneity: Arrays can only store elements of the same type.

Summary of Arrays in C

  1. Definition: An array is a collection of elements of the same data type stored in contiguous memory locations.
  2. Declaration: The syntax is data_type array_name[array_size].
  3. Indexing: Array elements are accessed using indices, starting from 0.
  4. Initialization: Arrays can be initialized at the time of declaration with specific values.
  5. Multidimensional Arrays: C supports arrays with more than one dimension, such as 2D arrays.
  6. Arrays and Pointers: Arrays are closely related to pointers, and you can use pointers to access array elements.
  7. Passing to Functions: Arrays are passed by reference to functions.
  8. Limitations: Arrays in C have a fixed size, lack automatic bound checking, and can only store elements of the same type.

Arrays are a powerful and essential tool in C programming, enabling efficient storage and manipulation of multiple values in a single variable.