How to create Two-Dimensional Arrays - C Array

C examples for Array:Multidimensional Arrays

Introduction

Two-dimensional arrays must be declared with two separate element numbers.

int iTwoD[3][3]; 

Two-dimensional arrays are accessed with two element numbers, one for the column and one for the row.

You can initialize a two-dimensional array in its declaration.

int iTwoD[3][3] = { {0, 1, 2}, {0, 1, 2}, {0, 1, 2} }; 

You can use looping structures to initialize your two-dimensional arrays.

Demo Code

#include <stdio.h> 
int main() /*from  w  w  w .j a v a  2 s.c o  m*/
{ 
   int iTwoD[3][3]; 
   int x, y; 
   //intialize the 2-d array 
   for ( x = 0; x <= 2; x++ ) { 
      for ( y = 0; y <= 2; y++ ) 
         iTwoD[x][y] = ( x + y ); 
   }
   //print the 2-d array 
   for ( x = 0; x <= 2; x++ ) { 
     for ( y = 0; y <= 2; y++ ) 
        printf("iTwoD[%d][%d] = %d\n", x, y, iTwoD[x][y]); 
   }
}

Result


Related Tutorials