C: Size of two dimensional array C: Size of two dimensional array arrays arrays

C: Size of two dimensional array


That's a problem of integer division!

int column = sizeof(result[0])/row;

should be

int column = 7 / 10;

and in integer division, 7/10==0.

What you want to do is divide the length of one row, eg. sizeof(result[0]) by the size of one element of that row, eg. sizeof(result[0][0]):

int column = sizeof(result[0])/sizeof(result[0][0]);


It's much more convenient (and less error prone) to use an array length macro:

#include <stdio.h>#define LEN(arr) ((int) (sizeof (arr) / sizeof (arr)[0]))int main(void){    char result[10][7];    printf("Number of rows: %d\n", LEN(result));    printf("Number of columns: %d\n", LEN(result[0]));    return 0;}


This works for me (comments explains why):

#include <stdio.h>int main() {   char result[10][7] = {       {'1','X','2','X','2','1','1'},       {'X','1','1','2','2','1','1'},       {'X','1','1','2','2','1','1'},       {'1','X','2','X','2','2','2'},       {'1','X','1','X','1','X','2'},       {'1','X','2','X','2','1','1'},       {'1','X','2','2','1','X','1'},       {'1','X','2','X','2','1','X'},       {'1','1','1','X','2','2','1'},       {'1','X','2','X','2','1','1'}   };    // 'total' will be 70 = 10 * 7   int total = sizeof(result);   // 'column' will be 7 = size of first row   int column = sizeof(result[0]);   // 'row' will be 10 = 70 / 7   int row = total / column;   printf("Total fields: %d\n", total);   printf("Number of rows: %d\n", row);   printf("Number of columns: %d\n", column);}

And the output of this is:

Total of fields: 70Number of rows: 10Number of columns: 7

EDIT:

As pointed by @AnorZaken, passing the array to a function as a parameter and printing the result of sizeof on it, will output another total. This is because when you pass an array as argument (not a pointer to it), C will pass it as copy and will apply some C magic in between, so you are not passing exactly the same as you think you are. To be sure about what you are doing and to avoid some extra CPU work and memory consumption, it's better to pass arrays and objects by reference (using pointers). So you can use something like this, with same results as original:

#include <stdio.h>void foo(char (*result)[10][7]){   // 'total' will be 70 = 10 * 7   int total = sizeof(*result);   // 'column' will be 7 = size of first row   int column = sizeof((*result)[0]);   // 'row' will be 10 = 70 / 7   int row = total / column;   printf("Total fields: %d\n", total);   printf("Number of rows: %d\n", row);   printf("Number of columns: %d\n", column);}int main(void) {   char result[10][7] = {       {'1','X','2','X','2','1','1'},       {'X','1','1','2','2','1','1'},       {'X','1','1','2','2','1','1'},       {'1','X','2','X','2','2','2'},       {'1','X','1','X','1','X','2'},       {'1','X','2','X','2','1','1'},       {'1','X','2','2','1','X','1'},       {'1','X','2','X','2','1','X'},       {'1','1','1','X','2','2','1'},       {'1','X','2','X','2','1','1'}   };   foo(&result);   return 0;}