Day#62 'C' Coding Challenge

Day#62 'C' Coding Challenge

What is the output of the below code?

#include<stdio.h> int m_array[2][2][2] = { { 1,2, 3,4 }, { 5,6, 7,8 } }; void main() { printf("%d\n%d\n%d\n%d\n",m_array,*m_array,**m_array,***m_array); }

------------------------------------------------------------------------------

The above code has a 3-dimensional array declared.
Let us start with a 1 dimensional array example to understand this

int a[5] = {10,4,5,6,7};

If we want to access first element we use a[0] or *a
If we want to access second element we use a[1] or *(a+1)

Similarly, for 2-dimensional array, as below

int a[2][3] = {{10,4,5},
                       {9,6,3}};

The above 2-dimensional array has 2 rows and 3 columns. 

If we want to access first element we use a[0][0] or **a
If we want to access second element we use a[0][1] or **(a+1)

Similarly, for 3-dimensional array, as in the code

int m_array[2][2][2] = {{1,2,3,4},
                       {5,6,7,8}};

The above 3-dimensional array has 2 sets arranged in 2 rows and 2 columns. 

If we want to access first element we use a[0][0][0] or ***a
If we want to access second element we use a[0][0][1] or ***(a+1)

Coming to the code, we were trying to print m_array,*m_array,**m_array,***m_array

Except ***m_array which prints the first element 1, all others print starting address of the array.

Post a Comment

0 Comments