C language content

Introduction to Arrays in C Language

An array in C is a collection of elements of the same type that are stored in contiguous memory locations. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

Key Characteristics of Arrays:

  • Fixed Size: The size of an array must be specified at the time of its declaration, and it cannot be changed during runtime.
  • Same Data Type: All elements in an array are of the same data type.
  • Index-Based: Arrays are indexed, starting from 0 to n-1 where n is the number of elements in the array.

Declaring Arrays

The syntax for declaring an array in C is:

				
					type arrayName[arraySize];

				
			
  • type: Data type of the elements in the array (e.g., int, float, char).
  • arrayName: Name of the array.
  • arraySize: Number of elements the array can hold.

Example:

				
					int numbers[5];  // Declares an array of 5 integers

				
			

Initializing Arrays

You can initialize an array at the time of declaration:

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

				
			

If you do not specify all elements, the remaining elements will be initialized to 0:

				
					int numbers[5] = {1, 2};  // Equivalent to {1, 2, 0, 0, 0}


				
			

Accessing Array Elements

Array elements are accessed using the index, which starts from 0:

				
					int firstNumber = numbers[0];  // Accesses the first element
int secondNumber = numbers[1]; // Accesses the second element


				
			

Example Program

Here’s a simple C program that demonstrates array declaration, initialization, and accessing elements:

				
					#include <stdio.h>

int main() {
    // Declare and initialize an array
    int numbers[5] = {10, 20, 30, 40, 50};
    
    // Access and print array elements
    for(int i = 0; i < 5; i++) {
        printf("Element at index %d: %d\n", i, numbers[i]);
    }
    
    return 0;
}

				
			

Arrays and Functions

Arrays can be passed to functions as arguments. However, when passing an array to a function, you actually pass a pointer to the first element of the array.

Example:

				
					#include <stdio.h>

void printArray(int arr[], int size) {
    for(int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    printArray(numbers, 5);  // Passing array to function
    
    return 0;
}

				
			

Multidimensional Arrays

C supports multidimensional arrays (arrays of arrays). The most common type is the two-dimensional array.

Declaration:

				
					type arrayName[rows][columns];

				
			

Example:

				
					int matrix[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

				
			

Accessing elements:

				
					int value = matrix[1][2];  // Accesses element in second row, third column

				
			

Example Program with Multidimensional Array

				
					#include <stdio.h>

int main() {
    // Declare and initialize a 2D array
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    
    // Access and print 2D array elements
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}


				
			

Common Operations on Arrays

  • Traversal: Iterating through each element using loops.
  • Insertion: Adding an element at a specific index (not straightforward in fixed-size arrays).
  • Deletion: Removing an element from a specific index (not straightforward in fixed-size arrays).
  • Searching: Finding the index of an element.
  • Sorting: Arranging elements in a specific order (ascending/descending).

Summary

Arrays in C provide a means to store multiple elements of the same type efficiently. They are fundamental data structures that serve as building blocks for more complex data structures like lists, stacks, and queues. Understanding how to declare, initialize, and manipulate arrays is essential for any C programmer.

Scroll to Top