As discussed in the previous post, there are two types of data structures available to C and C++ programmers. One is already built into the programming language such as arrays and structures. Other type of data structure is a bit complex in a sense that it can be implemented using the built in data structures and data types.

In C and C++ programming language, built in data structures include Arrays, Structures, Unions and Classes. Some of the examples of complex data structures are Stack, Queue, Linked List, Tree and Graph. Some more advance data structures include Trie Data Structure, Binary Decision Diagram (BDD), Ordered Binary Decision Diagrams (OBDDs) and Multi-valued Decision Diagram (MDDs).

The aim of this tutorial is to teach you how to declare, initialize and use simple arrays as well as multidimensional arrays. You will also be able to use arrays as data structure in your C/C++ program. After going through this tutorial you will be able to answer the following questions.

  • What is an array and how you can use it?
  • How to declare and initialize simple arrays?
  • How to declare and initialize multidimensional arrays?
  • How to perform simple operations on arrays?

What is an array?

Array is a very basic data structure provided by every programming language. Let’s talk about an example scenario where we need to store ten employees’ data in our C/C++ program including name, age and salary. One of the solutions is to declare ten different variables to store employee name and ten more to store age and so on. Also you will need some sort of mechanism to get information about an employee, search employee records and sort them. To solve these types of problem, C and C++ provide a mechanism called Arrays.

Array Definition

An array is simply a number of memory locations, each of which can store an item of data of the same data type and which are all referenced through the same variable name. Ivor Horton.

Array may be defined abstractly as finite order set of homogeneous elements. So we can say that there are finite numbers of elements in an array and all the elements are of same data type. Also array elements are ordered i.e. we can access a specific array element by an index.

How to declare an array in C?

The general form of declaring a simple (one dimensional) array is:
array_type variable_name[array_size];
So in your C/C++ program you can declare an array like
int Age[10];
Here array_type declares base type of array which is the type of each element in array. In our example array_type is int and its name is Age. Size of the array is defined by array_size i.e. 10. We can access array elements by index, and first item in array is at index 0. First element of array is called lower bound and its always 0. Highest element in array is called upper bound.

In C upper and lower bounds cannot be changed during the execution of the program, so array length can be set only when the program in written.

Age 0

Age 1

Age 2

Age 3

Age 4

Age 5

Age 6

Age 7

Age 8

Age 9

30

32

54

32

26

29

23

43

34

5

Array has 10 elements

Note: One good practice is to declare array length as a constant identifier. This will minimize the required work to change the array size during program development.

Considering the array we declared above we can declare it like

How to initialize an array in C?

Initialization of array is very simple in c programming. There are two ways you can initialize arrays.

  • Declare and initialize array in one statement.
  • Declare and initialize array separately.

Look at the following C code which demonstrates the declaration and initialization of an array.

Array can also be initialed in a ways that array size is omitted, in such case compiler automatically allocates memory to array.
int Age [] = {30, 22, 33, 44, 25};
Let’s write a simple program that uses arrays to print out number of employees having salary more than 3000.

Program to demonstrate arrays in C

Program to demonstrate arrays in C++

How to declare and initialize multidimensional arrays?

Often there is need to manipulate tabular data or matrices. For example if employee salary is increased by 20% and you are required to store both the salaries in your program. Then you will need to store this information into a two dimensional arrays. NIST defines two dimensional array as Matrix : “A two-dimensional array. By convention, the first index is the row, and the second index is the column”.

C and C++ languages gives you the ability to have arrays of any dimension.

Multidimensional Arrays in C

Consider the example above, you have to store “previous salary”, “present salary” and “amount of increment”. In that case you will need to store this information in three dimensional arrays.

First I will show you how to declare a two dimensional array and initialize it. Then write a complete program to use multidimensional arrays.

This defines an array containing 10 elements of type int. Each of these elements itself is an array of two integers. So to keep track of each element of this array is we have to use two indices. One is to keep track of row and other is to keep track of column.

Elements of multidimensional arrays

A matrix (multidimensional array)  is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns as shown below. In computer science, a matrix is a two dimensional array which is indexed by two subscripts i.e. one for row and another for column.

Here is a graphical view of multidimensional array that we use to store salary and increment on salary. First column stores the salary element of the array and second column stores increment on salary. We could add another column to store the new salary which adds the increment to the salary.

The dimensions of matrix below are 10 × 2, because there are two rows and three columns.

Column 0 – Salary

Column 1 – Increment

Row 0
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8
Row 9
Row 10

Initializing multidimensional arrays

Multidimensional arrays can also be initialized in two ways just like one dimensional array. Two braces are used to surround the row element of arrays.

If you are initializing more than one dimension then you will have to use as many braces as the dimensions of the array are.

Here is a complete program written in both C and C++ to demonstrate the use of multidimensional arrays. You can find the whole source code in a zip file at the end of the tutorial. Source code is available in both C and C++ programming languages. Zip file also contains the demonstration of three dimensional arrays.

Demonstration of two dimension arrays

The code below demonstrates two dimension arrays. It uses the same example of employee salary to increment it by 20% and adds it to actual salary then print current salary, increment and new salary.

Two dimensional Array in C

This program shows how we can declare, traverse and print array items in C.

Two dimensional array in C++

This program shows how we can declare, traverse and print array items in C++.

Download Complete Source Code:

  Arrays as Data Structure (2.2 KiB, 10,307 hits)

Related C programming tutorials and source code on Arrays