Arrays are one of the most fundamental data structures in C. They allow you to store multiple values of the same data type under a single variable name. This makes data processing easier, especially when working with lists, tables, or large sets of numerical values.
1. Defining Arrays in C
An array is a collection of elements stored in contiguous memory locations. All elements must be of the same data type.
Syntax
data_type array_name[size];
Example
int marks[5]; // array of 5 integers
char name[20]; // array of characters (string)
float prices[10];
Initialization
Arrays can be initialized at the time of declaration.
int nums[5] = {10, 20, 30, 40, 50};
If you omit the size, C automatically adjusts:
int nums[] = {10, 20, 30, 40, 50};
2. Processing Arrays
Processing means accessing and manipulating array elements using indexing.
Index starts from 0 and goes up to size-1.
Example: Reading & Printing Array Elements
#include <stdio.h>
int main() {
int i, arr[5];
printf("Enter 5 numbers:\n");
for (i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
printf("You entered:\n");
for (i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
3. Passing Arrays to Functions
In C, arrays are always passed by reference, meaning the function receives the memory address of the first element.
Function Declaration
void display(int arr[], int size);
Example
#include <stdio.h>
void display(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int nums[5] = {10, 20, 30, 40, 50};
display(nums, 5);
return 0;
}
Key Point
- Any changes inside the function affect the original array.
4. Multi-Dimensional Arrays
A multi-dimensional array is an array of arrays.
The most commonly used is the 2-D array, similar to matrices or tables.
4.1 Defining a 2-D Array
Syntax
data_type array_name[rows][columns];
Example
int matrix[3][3];
Initialization
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
4.2 Accessing 2-D Array Elements
printf("%d", matrix[1][2]); // Output: 6
4.3 Processing a 2-D Array (Nested Loops)
#include <stdio.h>
int main() {
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
4.4 Passing a 2-D Array to a Function
You must specify the column size.
void printMatrix(int arr[][3], int rows);
Example
#include <stdio.h>
void printMatrix(int arr[][3], int rows) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int main() {
int matrix[2][3] = {{1,2,3},{4,5,6}};
printMatrix(matrix, 2);
return 0;
}
Summary
| Concept | Description |
|---|---|
| Defining Arrays | Declaring a list of items of the same type |
| Processing Arrays | Access using loops and indices |
| Passing Arrays to Functions | Arrays passed by reference |
| Multi-Dimensional Arrays | Arrays of arrays, commonly 2-D arrays |
๐ Citations
๐ View other articles about C Programming:
https://savanka.com/category/learn/c-programming/
๐ External C Documentation:
https://www.w3schools.com/c/