This works for me (comments explain 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 result of this:
Total of fields: 70 Number of rows: 10 Number of columns: 7
EDIT:
As @AnorZaken pointed out, passing the array of the 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 an argument (not a pointer to it), C will pass it as a copy and apply some C magic between them, so you won’t go through exactly as you consider yourself. To be sure of what you are doing, and to avoid additional processor work and memory consumption, it is better to pass arrays and objects by reference (using pointers). So you can use something like this, with the same results as the 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; }