Three-dimensional (3D) array in C |Introduction to 3D Array in C

An Array is a group of elements with the same (homogeneous) data type. A 3D array is a multi-dimensional array. A multidimensional array is basically an array of arrays.

 We can declare the 3-D array as:

int a[2][3][2];

The meaning of the above representation can be understood as:

  1. The memory allocated to variable a is of data type int.
  2. The total capacity that this array can hold is 2*3*2, which is equal to 12 elements.
  3. The data is being represented in the form of 2 arrays with 3 rows and 2 columns each.

In 3D arrays representation, the first square bracket represents the level of the array that has to be considered, second would be the number of rows, and the third one is for the number of columns.

The index representation of the array for the first element always starts with zero and ends with size-1. So, for example, if the number of rows is 3, then the index representation for accessing the data in rows will be 0, 1 and 2. The same logic applies for the array level and column indexes too. For the above representation, to get the data of 1st level of the array with 2nd row 2nd  column, we can access by a[0][1][1].

Initializing 3D Arrays in C:

We can initialize a 3D array similar to the 2-D array.

int a[2][3][2]={ { { 0, 1 }, { 2, 3 }, { 4, 5 } }, { { 6, 7 }, { 8, 9 }, { 10, 11 } } };

Programming Example:

Example-1: Program to print elements of a three-dimensional (3D) array.

Output:

Example-2: program to read and print elements of a three-dimensional (3D) array.

Output:

10 thoughts on “Three-dimensional (3D) array in C |Introduction to 3D Array in C”

  1. you are in reality a just right webmaster The site loading velocity is incredible It seems that you are doing any unique trick In addition The contents are masterwork you have performed a wonderful task on this topic

    Reply
  2. Newtoki You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!

    Reply

Leave a Comment