What are the Types of Array in C? - Scaler Topics (original) (raw)

Arrays are classified into two types based on their dimensions : single-dimensional and multi-dimensional. Logically, a single-dimensional array represents a linear collection of data, and a two-dimensional array represents a mathematical matrix. Similarly, a multidimensional array has multiple dimensions.

All elements in the memory are stored in a linear order; it's simply a concept called memory addressing that allows us to access the array dimension-wise.

Single Dimensional Array

The single-dimensional array is one of the most used types of the array in C. It is a linear collection of similar types of data, and the allocated memory for all data blocks in the single-dimensional array remains consecutive.

Syntax for Declaration of Single Dimensional Array

Below is the syntax to declare the single-dimensional array.

Initialization of Single Dimensional Array

We can initialize an array by providing a list of elements separated by commas and enclosed by curly braces.

Important Point: What if we provide fewer elements than the size of the array?

In this scenario, the rest of the memory block will be filled with the default value, which is null for string array, 0 for integer array, etc. See the example below for further illustration.

Output :

Arrangement

arrangement of Single Dimensional Array

Accessing Elements of Single Dimensional Array

We can access the element of the single-dimensional array by providing the index of the element with the array name. The index of the array starts with zero.

Example

C Program to Enter 5 Numbers and Print them in Reverse Order.

Output :

Explanation :

Note :

To learn more about one-dimensional arrays follow this link.

Multi-Dimensional Array

Multidimensional arrays are an advanced version of a single-dimensional array that can be nested up to multilevel. From another perspective, you can think about it as, an array of an array with one less dimension i.e. an array of a one-dimensional array is a two-dimensional array or an array of a two-dimensional array is a three-dimensional array.

Syntax for Declaration of Multi-Dimensional Array

The syntax of a multidimensional array is only a little bit different from the single-dimensional array.

Two-Dimensional Array

A two-dimensional array is a specialized form of a multidimensional array which have two dimensions. It is widely used among all types of arrays in C programming to represent a matrix in a data structure.

Syntax and Declaration of Two Dimensional Array

array2d is an int type array having 5 rows and 10 columns.

Initialization of Two-Dimensional Array

A two-dimensional array can be initialized by providing a list of the single-dimensional arrays in proper syntax,

Arrangement

arrangement of Two Dimensional Array

Accessing Individual Elements of Two-Dimensional Array

The individual element can be accessed by providing both row and column index of the array,

Passing the Entire 2D Array

There exist different techniques to pass the several types of arrays in C to a function, a few of them are enumerated below, A 2D array can be passed to a function using the following techniques,

1. When Row and Column Both Sizes are Globally Available

Note : We can also skip the row size in the function parameter.

Output :

Explanation :

2. When Column Size is Globally Available

We can omit the Row Size while receiving the parameter in the function definition.

Output :

Explanation :

3. Using Pointer Approach

In the function definition, a pointer can be used to access the memory of the passed array during the function call.

Output :

Explanation :

Passing 2D Array as Parameters to Functions

Passing a Row

The function definition can accept a single-dimensional array, and we can provide the array by accessing the row during the function call.

Output :

Explanation :

Passing Individual Elements

We can simply access the individual elements of the 2D array and give it to the function call.

Output :

Explanation :

Example

C Program to find the sum of all the elements of a 2x32 x 3 matrix.

Output :

Explanation :

Three-Dimensional Array

Three-dimensional arrays are also a specialized form of a multidimensional array, generally, they are used to represent three-dimensional coordinates in programming. We could also say that three-dimensional arrays are an array of two-dimensional arrays.

Syntax for Declaration of Three Dimensional Array

Example

C program to show the concept of a three-dimensional array.

Input :

Output :

Representation : representation of Three Dimensional Array

Conclusion