Use the sizeof operator to work out the number of elements in each dimension of a multidimensional array. - C Array

C examples for Array:Multidimensional Arrays

Introduction

Here's the previous loop using the sizeof operator to compute the loop control limits:

Demo Code

#include <stdio.h> 

int main(void) { 

    int numbers[2][3][4] = {
                           {                       // First block of 3 rows
                             { 10, 20, 30, 40 },
                             { 15, 25, 35, 45 },
                             { 47, 48, 49, 50 }
                           },//from   www.j  a va  2  s. c o m
                           {                       // Second block of 3 rows
                             { 10, 20, 30, 40 },
                             { 15, 25, 35, 45 },
                             { 47, 48, 49, 50 }
                          }
                       };


    int sum = 0;

    for(int i = 0 ; i < sizeof(numbers)/sizeof(numbers[0]) ; ++i)
    {
      for(int j = 0 ; j < sizeof(numbers[0])/sizeof(numbers[0][0]) ; ++j)
      {
        for(int k = 0 ; k < sizeof(numbers[0][0])/sizeof(numbers[0][0][0])  ; ++k)
        {
          sum += numbers[i][j][k];
        }
      }
    }

  printf("The sum of the values in the numbers array is %d.", sum);
  return 0; 
}

Result


Related Tutorials