Arrays in C Programming: A Beginner's Guide
Learn Arrays in C Programming from Scratch
Introduction
Arrays are a kind of Data Structure, Data Structure is simply a way of organizing data.
The array is a data structure that stores a collection of data elements of the same data type in which a continuous block of memory. The continuous block of memory is a memory block with memory for each of the elements that are next to each other, and thus this makes it easier for the CPU to access.
In an array, we can store more than one data item of the same data type. So let's say we have about 10 scores of students in a test, how do we go about that?
That is where the array comes in, since the scores are all of the same data type which is an integer, then we can use an array, as it will save more space and make the code more efficient instead of just declaring 10 different variables to hold the scores for the 10 different students.
Declaration and Initialization of Arrays
Before we can store an element or piece of data in an array, we will need to first declare and initialize the array. We can decide to declare the array first before initializing it, and we can decide to declare and initialize it at a time.
Declaration of an Array
The Declaration of the array is done using a data type of the elements that are to be stored in the array, followed by the variable name and a square bracket. Inside the square bracket, the number of elements that are to be stored in the array is defined there. For example, if we are going to store the test scores of 10 students, then we will put the number 10 inside the square bracket. This is not only meant for numbers alone, let's say we want to store the names of 10 students, inside the square bracket, we will have to put 10 too, because that is the number of items we want to store in the array. For example:
#include <stdio.h>
int main(void)
{
// declaration of an array to store scores of 10 students
int stu_test_scores[10];
// declaration of an array to store names of 10 students
char stu_names[10];
return (0);
}
The first declaration is an array that is ready to take 10 different integers. While the second declaration is an array that is ready to take 10 different string characters.
Initialization of an Array
The initialization of an array can be done in different ways, but one thing that is general with any method whatsoever is that the elements that are to be stored in the array are all enclosed in curly braces and each element is separated from each other by a comma.
The three ways arrays can be initialized are:
- An array can be Initialized immediately after declaration. For example:
#include <stdio.h>
int main(void)
{
// declaration of an array to store scores of 5 students
int stu_test_scores[5];
// initialization of the array
stu_test_scores[5] = { 12, 8, 25, 30, 5 };
return (0);
}
Arrays can be Initialized using their index: Every array starts with an index of 0, the index of an array is simply the position of the individual elements that are in the array. For example, starting with index 0 as the first position, index 1 as the second position and so on till the last element.
So to initialize arrays through this method, we simply just have to declare the array at each of the indexes and set the element that is supposed to be at the particular index as the value, and that is it. For example:
#include <stdio.h> int main(void) { // declaration of an array to store scores of 5 students int stu_test_scores[5]; // initialization of the array stu_test_scores[0] = 12; stu_test_scores[1] = 8; stu_test_scores[2] = 25; stu_test_scores[3] = 30; stu_test_scores[4] = 5; return (0); }
The last way of initializing an array that I will be talking about is simply one that wouldn't require you to declare the number of the elements that the array is going to carry, the array will do that itself.
In this case, the declaration and initialization have to be done immediately. The two steps have to be done together. For example:
#include <stdio.h>
int main(void)
{
// declaration and initialization of an array
int stu_test_scores[] = { 12, 8, 25, 30, 5 };
return (0);
}
So when the number of elements that an array is carrying is not included in the name of the array, it has to be initialized immediately after it is declared, else it will throw an error at compilation.
Accessing Each Element of an Array
Since an array is used to store a collection of data of the same data type as a single collection, we will, however, need to access such data for various reasons in our C Program, either for manipulation or simply for use in other parts of our program, for instance in a situation where we have the scores of students, we might as well need to access these scores to perform some arithmetic calculations on them such as to find the average etc. So in that case there is a need for us to be able to access the elements of the array individually.
This is where indexing comes into play. Each element of an array occupies a position in the array, this position starts from 0 for the first element in the array, 1 for the second one, 2 for the third one and the lists go on and on and on until one gets to the end of the array list.
This image is a typical example of the relationship between the array elements and their indexes. The index of an array element is always one number less than the position of the array element as the array index starts counting from 0. To access the elements of the array, the name of the array with a square bracket and the index of the element which is to be accessed is passed to the square bracket.
For example:
#include <stdio.h>
int main(void)
{
// declaration and initialization of an array
int stu_test_scores[6] = { 2, 4, 10, 5, 15, 3 };
printf("stu_test_scores[0]: %d\n", stu_test_scores[0]); // gives 2
printf("stu_test_scores[1]: %d\n", stu_test_scores[1]); // gives 4
printf("stu_test_scores[2]: %d\n", stu_test_scores[2]); // gives 10
printf("stu_test_scores[3]: %d\n", stu_test_scores[3]); // gives 5
printf("stu_test_scores[4]: %d\n", stu_test_scores[4]); // gives 15
printf("stu_test_scores[5]: %d\n", stu_test_scores[5]); // gives 3
return (0);
}
The code above prints each of the elements of the array stu_test_scores[6] on a new line. The code above will give us:
Of course, instead of having so many printfs in our code, we can further optimize it and reduce it to just a single printf by using either a while-loop or a for-loop.
Here is a refactored code for this:
#include <stdio.h>
int main(void)
{
// declaration and initialization of an array
int stu_test_scores[6] = { 2, 4, 10, 5, 15, 3 };
int i = 0;
while (i < 6)
{
printf("stu_test_scores[%d]: %d\n", i, stu_test_scores[i]);
i++;
}
return (0);
}
With the above code, you have a more clean program that does the same thing as the one before it.
Accessing each element of the array gives room for so many possibilities which we will talk about in our subsequent articles going forward.
Conclusion
There is still more to arrays, they are connected to string literals as well as pointers, all these will be explained in detail alongside examples and applications in the subsequent articles that will be published.
Thank you for reading, you can connect with me on Twitter and LinkedIn.