How to work with arrays in C – Programming

How to work with arrays in C – Programming


Arrays

In all the programs we have done until now, to store and operate on values we have used variables. But at a point in time, a variable can hold only a single value. For example, in the following syntax: int a = 10; we are able to store only 10 in the variable a, which is a single value. It is normal in programming to work with a list of values or a group of values at once. For such purposes, variables cannot be used. So, C language provides the construct array for holding multiple values at once.
Definition: “An array is a sequential collection/group of homogeneous (same type) elements which are referred by the same name”. The type refers to the data types like int, char, float etc. All the elements/values in the array are of the same type (data type). We cannot store values of different data types in the same array.
Uses:
  1. To maintain a list of values.
  2. To maintain a table of values.

One Dimensional Array

For maintaining a list of items in C, we can declare an array with a single subscript like ai . Such arrays with only a single subscript are known as one dimensional arrays. Common uses of one dimensional arrays are: to maintain a list of numbers, to maintain the list of marks, to maintain a list of student names etc.

Declaration of one dimensional array

For using arrays in C programs, just like we are declaring variables before using them, we should declare arrays before using them. The syntax for declaring a one dimensional array is as shown below:
array-decl
Example:
array-example
In the above example, int is the data type, a is the array name and size is the number of elements that we can store in the array. So, in the above example ais an integer array, which can hold 10 integer values. All the 10 elements in the array will be stored sequentially one after another inside the main memory (RAM). The one dimensional array declared above will be maintained in the memory as shown below:
one-dim-array

Initialization of one dimensional array

Initialization means assigning values. To assign values to the elements in the array, we use the following syntax:
array-init-syntax
Example:
array-init-example
In the above syntax, index refers to the element in the array. The index of an array always starts with zero. So, the index values for the array a in the above example are: 0, 1, 2, 3, 4. If we want to access nth element in the array, theindex value will be n-1. For example, if we want to refer first element in the array, the index value will be 1-1=0. In, the above example, we are assigning value 10 to the first element of the array. 
Note: If the array elements are not initialized, the values stored in the elements of the array will be garbage values.
Generally, there are two types of initializing an array. They are: 1) Static Initialization (at compile time) and 2) Dynamic Initialization (at run time). In static initialization, the elements of an array are assigned values when the program is compiled. In dynamic initialization, the elements of an array are assigned values when the program is executed (runtime). The above way of initialization is static initialization. We can also perform static initialization in other ways as shown below:
array-init-syn
Example:
array-init-ex
In the above example, in the first line, the size is specified as 10. But, in the second line, the size was not specified. In such cases, the size is automatically calculated. In this example, the size is automatically computed as 10 by the compiler.
In dynamic initialization, the values are assigned to the elements of the array during the execution of the program. Let’s see the following piece of code:
In the above example, the user will be storing the values into the array while executing the program. Until then, garbage values will be stored in the array.
Note: If we use braces i.e., { and }, for initializing the array, the default values for all the elements in the array will be zero.

Two Dimensional Array

An array that has two subscripts, for example: aij is known as a two dimensional. A two dimensional array is used to store a table of values which has rows and columns. Some of the uses/applications of the two dimensional arrays are: to maintain the marks of students, to maintain prices of items etc…

Declaration of a two dimensional array

Like a one dimensional array, the two dimensional array must also be declared before using it in the program. The syntax for declaring a two dimensional array is shown below:
2d-array-syn
Example
2d-array-ex
In the above example, int refers to the data type, a refers to the array name, first set of square brackets represents the number of rows and the second set of square brackets represents the number of columns. So, our two dimensional array contains 9 elements in total.

Initialization of a two dimensional array

A two dimensional array can be initialized in different ways either statically at compile time or dynamically at run time. The syntax for initializing a two dimensional array statically is as shown below:
2d-init-syn
Example
2d-init-ex
The memory representation of the two dimensional array will be as shown below:
2d-array-memory
The values will be assigned starting with the first element in the first row and so on. If insufficient values are provided, then the remaining elements will be initialized to zero. There is another way of initializing a two dimensional array in which we can specify the values for each row separately. We can see the example below:
2d-array-init
Take your time to comment on this article.

Comments