15 – Arrays in C Programming language

In this chapter, we will talk about Arrays.


Array is a kind of data structure which can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables such as num0, num1………….num99, declare one array variable such as numbers or use numbers[0], numbers[1]…………….numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of adjacent memory locations.The lowest address corresponds to the first element & the highest address to the last element.

Declaring Arrays

To declare an array in C, a programmer specifies the type of the elements & the number of the elements required by an array as follows:

This is called a single-Dimensional array. The array size must be an integer constant greater than zero & type can be any valid C data type.
For Example : to declare a 10 elements array called balance of type double, then use this statement:

Here balance is variables array which is sufficient to hold up to 10 double numbers.

Initializing Array

In C, you can initialize an array either one by one or using a single statement as follows:

The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].

If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write:

You will create exactly the same array as you did in the previous example. Following is an example to assign a single element of the array:

The above statement assigns the 5th element in the array with the value of 50. All the arrays have 0 as the index of their first element which is also called the base index & the last index of an array will be total size of the array minus 1.
The pictorial representation of the array we discuss above is given below:

Accessing Array Elements

An element is accessed by indexing the name of an array. This is done by placing the index of the element after the name of array within square brackets.
For Example:

The above statement will take the 10th element from the array & assign the value to salary variable. The below example shows that how to use all the three above mentioned concepts that are declaration, assignment & accessing arrays:

when above code is compiled & executed, it produces the following result:

Arrays in Detail

1Multi-dimensional arraysThe simplest form of multi dimensional array is two dimensional array.
2Passing arrays to Functions
3Return array from a FunctionC allows a function to return an array.
4Pointer to an array

Subscribe our channels:

Click Here for more Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.