Disciplined storage: Arrays
Arrays in C are collections of elements of the same type, stored in contiguous memory locations. Let's go through various aspects of arrays with sample code and comments for explanation:
#include <stdio.h>
int main() {
// Declaring an array
int numbers[5];
// Initializing an array
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
// Alternatively, declaring and initializing in one step
// int numbers[] = {1, 2, 3, 4, 5};
// Finding the size of the array
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Size of the array: %d\n", size);
// Accessing elements of the array
printf("First element: %d\n", numbers[0]);
printf("Third element: %d\n", numbers[2]);
// Iterating through the array using a loop
printf("Array elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
// Initializing an array with a specific size and value
int zeros[3] = {0}; // Initializes all elements to 0
printf("Array with zeros: %d %d %d\n", zeros[0], zeros[1], zeros[2]);
// Multi-dimensional arrays
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// Accessing elements in a multi-dimensional array
printf("Matrix element at (1, 2): %d\n", matrix[1][2]);
return 0;
}
Explanation with comments:
Array Declaration:
int numbers[5];
This declares an integer array named
numbers
with a size of 5.Array Initialization:
numbers[0] = 1; // ... numbers[4] = 5;
Initializes each element of the array individually.
Array Declaration and Initialization in One Step:
int numbers[] = {1, 2, 3, 4, 5};
This declares and initializes the array in a single line. The size is automatically determined based on the number of elements provided.
Finding Array Size:
int size = sizeof(numbers) / sizeof(numbers[0]);
Calculates the size of the array by dividing the total size of the array by the size of one element.
Accessing Array Elements:
printf("First element: %d\n", numbers[0]); printf("Third element: %d\n", numbers[2]);
Accessing individual elements of the array using their indices.
Iterating Through an Array:
for (int i = 0; i < size; i++) { printf("%d ", numbers[i]); }
Using a loop to iterate through the array and print its elements.
Initializing Array with a Specific Size and Value:
int zeros[3] = {0};
Initializes all elements of the array to 0. The size is specified, and if there are fewer values provided than the size, the remaining elements are initialized to 0.
Multi-dimensional Arrays:
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };
Declares a 2x3 matrix.
Accessing Elements in a Multi-dimensional Array:
printf("Matrix element at (1, 2): %d\n", matrix[1][2]);
Accessing individual elements in a multi-dimensional array using indices.
Let's use the matrix
array defined in the previous example and demonstrate how to access its elements using nested loops:
#include <stdio.h>
int main() {
// Multi-dimensional array (2x3)
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// Accessing elements using nested loops
printf("Matrix elements:\n");
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 3; col++) {
printf("%d ", matrix[row][col]);
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Explanation with comments:
Nested Loops:
for (int row = 0; row < 2; row++) { for (int col = 0; col < 3; col++) { printf("%d ", matrix[row][col]); } printf("\n"); // Move to the next line after each row }
Nested loops are used to iterate through each element of the two-dimensional array. The outer loop (
row
) iterates over the rows, and the inner loop (col
) iterates over the columns.Accessing Matrix Elements:
printf("%d ", matrix[row][col]);
This line prints the value of the current element at the specified row and column of the matrix.
Newline After Each Row:
printf("\n");
After printing all the elements in a row, a newline character is printed to move to the next line for the next row.
When you run this program, it will output:
Matrix elements:
1 2 3
4 5 6
These examples cover various aspects of working with arrays in C, including declaration, initialization, accessing elements, iterating, and multi-dimensional arrays.
Subscribe to my newsletter
Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jyotiprakash Mishra
Jyotiprakash Mishra
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.