Difference between 1D and 2D arrays in C Programming

Difference between 1D and 2D arrays in C Programming

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

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 a is 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-1

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, the index 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:

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:
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-1
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-1

Comments