Arrays in C Programming – C and C++ Programming


What is an Array in C Language?

An array in C Programing Language can be defined as number of memory locations, each of which can store the same data type and which can be references through the same variable name. Click here for the introduction to arrays.

An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all floats or all characters etc. Usually, the array of characters is called a “string”, where as an array of integers or floats is called simply an array. All elements of any given array must be of the same type i.e we can’t have an array of 10 numbers, of which 5 are int and 5 are float.

Arrays and pointers have a special relationship as arrays use pointers to reference memory locations.

Declaration of an Array

Arrays must be declared before they can be used in the program. Standard array declaration is as

Here type specifies the variable type of the element which is going to be stored in the array. In C programming language we can declare the array of any basic standard type which C language supports. For example

In C Language, arrays starts at position 0. The elements of the array occupy adjacent locations in memory. C Language treats the name of the array as if it were a pointer to the first element This is important in understanding how to do arithmetic with arrays. Any item in the array can be accessed through its index, and it can be accessed any where from with in the program. So

variable m will have the value of first item of array height.

The program below will declare an array of five integers and print all the elements of the array.

Initializing Arrays

Initializing of array is very simple in c programming. The initializing values are enclosed within the curly braces in the declaration and placed following an equal sign after the array name. Here is an example which declares and initializes an array of five elements of type int. Array can also be initialized after declaration. Look at the following C code which demonstrate the declaration and  initialization of an array.

Performing operations on Arrays

Here is a program that will demonstrate the simple operations of the array.

Here is a more complex program that will demonstrate how to read, write and traverse the integer arrays

Copy one array into another

There is no such statement in C language which can directly copy an array into another array. So we have to copy each item separately into another array.

To summarize, arrays are provides a simple mechanism where more than one elements of same type are to be used. We can maintain, manipulate and store multiple elements of same type in one array variable and access them through index.

Multidimensional Arrays

In C Language one can have arrays of any dimensions. To understand the concept
of multidimensional arrays let us consider the following 4 X 5 matrix

Row number (i) Column numbers (j)
0 11 3 5 -9 -6
1 5 6 -8 7 24
2 -8 9 2 12 45
3 10 13 -10 4 5

Let us assume the name of matrix is x

To access a particular element from the array we have to use two subscripts on for row number and other for column number the notation is of the form X [i] [j] where i stands for row subscripts and j stands for column subscripts. Thus X [0] [0] refers to 10, X [2] [1] refers to 16 and so on In short multidimensional arrays are defined more or less in the same manner as single dimensional arrays, except that for subscripts you require two squire two square brackets. We will restrict our decision to two dimensional arrays.

Below given are some typical two-dimensional array definitions

The first example defines tables as a floating point array having 50 rows and 50 columns. the number of elements will be 2500 (50 X50).

The second declaration example establishes an array line of type character with 24 rows and 40 columns. The number of elements will be (24 X 40) 1920 consider the following two dimensional array definition int values [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 11, 12, };

Here the first subscript stands for the row number and second one for column number. First subscript ranges from 0 to 2 and there are altogether 3 rows second one ranges from 0 to 3 and there are altogether 4 columns.

Alternatively the above definition can be defined and initialized as

Here the values in first pair of braces are initialized to elements of first row, the values of second pair of inner braces are assigned to second row and so on. Note that outer pair of curly braces is required. If there are two few values within a pair of braces the remaining elements will be assigned as zeros.

Here is a sample program that stores roll numbers and marks obtained by a student side by side in matrix

The above example illustrates how a two dimensional array can be read and how the values stored in the array can be displayed on screen. For Tutors contact details click here.





Arrays in C Programming

One thought on “Arrays in C Programming – C and C++ Programming

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s