arrays in C

C series for 1st sem students: Arrays in C

Posted on Posted in Blogs, Technology

Today we are going to discuss about arrays in C. Arrays are a type of data structure that can store a fixed-size sequential collection of elements of the same type. In order to understand arrays we must first understand the meaning of the highlighted words of the above definition in terms of programming.

Data structure: In programming data structure is the organisation of data in a specific format. In a real world analogy, if rice grains are data, then a weighted bag (in Kg) can be its data structure, if drops of milk are data, then a litre bottle is its data structure and if bananas are data, then a dozen will be its data structure. A data structure also represents the relationships among the data, and the functions or operations that can be applied to them.

Fixed-size: Arrays in C are fixed sized data structure, unlike some modern languages. The size of the array must be determined during the compilation process and that much memory (in RAM) must be reserved for it. You may wonder why we are concerned about a few bytes of memory when RAMs are usually 8gb and above, in current PCs. But C language was developed about 50 years ago. At that time RAMs were in order of kilobytes. Even in mid 70s, one of the first portable computer, IBM 5100 had 16–64 kiB RAM, i.e. 128kb of RAM. That’s why C was designed in such a way that the programmer can manage the memory themselves in the code. This is no longer an issue with modern computers, so modern programming languages don’t require programmers to manage memory. We will discuss memory management in details in future blogs.

Sequential: The individual data in an array are saved sequentially in the memory. So we can move through the memory addresses to operate on the individual data. The memory locations are accessed by use of what is known as pointers. Pointers are like markers on a map, pointing to a particular location. They help a programmer access and manage memory. Since modern languages like javascript, don’t have explicit memory management, pointers are not available in them. Still the concept of pointers is one of the most important concepts in programming. We’ll discuss pointers in a lot of future blogs.

Same type: The elements of an array must be of the same datatype. For example if we want to store the marks of a student, then we use an integer array. Here all individual marks should be integer values. If any of those marks is a fractional value, then the array will have to be a float or double type array, and all the elements should be float/double. This is because different data types take up different amounts of memory. While traversing through the array, the program iterates over the fixed byte size as indicated by the data type. In case of int arrays, it will iterate over 4 bytes, while in char arrays, it will iterate over every single byte.


Syntax of arrays:

To declare an array, the syntax is:
type arrayName [ arraySize ];
For example, to store the marks of a student we can write:
double marks[5] = {100.0, 72.5, 34.0, 70.0, 50.0};
To access a particular value in the array, we indicate its index (starting from 0) in square brackets with the variable name.
double maths = marks[2];
Here maths corresponds to the 3rd element of the marks array, since indices start at 0.

Let us see this simple example of storing first five natural numbers in an array and calculating their sum.

In the integer-typed array myArray, we are storing the first 5 natural numbers. We then declared an integer variable to hold the sum of these numbers. In a for loop (discussed in this previous blog) we iterate over the elements of the array and add the individual elements to the sum variable in a successive manner.

pass array in functions in CArrays can also be passed into functions as arguments. But in this approach, the length of the array also needs to be passed, because the function cannot determine the length of the array that is passed into it. Lets see that in action using the same example. Here in line 14 we pass the array and the length of the array in the function calcSum. The function calculates the sum in the same manner, by using a for loop, and returns a integer value which is assigned to the variable sumOfArray while calling calcSum function.

Arrays can be multi-dimensional too. The simplest example will be an array of words. A single word is an array of characters. So to have an array of words, we need a two dimensional array of characters. In the following example we take a two dimensional array names, which hold names of six people. We iterate over both the dimensions in separate for loops to print out the names and letters of each name in each iteration. Through this example you might notice that there is no string datatype in C. Strings are just arrays of characters. In many modern languages like javascript, however, strings are a valid data type.

using arrays for strings


Multi-dimensional arrays are often used in mathematical operations like matrix operations. The iteration of two dimensional arrays using two for loops is a pretty standard way to do matrix calculations. In subsequent blogs we’ll discuss more about arrays, but only after we make a good foundation of our knowledge of pointers.

You can download the two dimensional array code { here }

If you liked this article please comment and show your support and interest so that I’ll be motivated to continue this effort. Like our facebook page if you haven’t already. And if you have any questions please comment. I’ll try to reply all.