C: Two-dimensional array size

I need help counting the rows and columns of a two-dimensional array. I can't seem to count the columns?

#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'} }; int row = sizeof(result) / sizeof(result[0]); int column = sizeof(result[0])/row; printf("Number of rows: %d\n", row); printf("Number of columns: %d\n", column); } 

Output:
Number of lines: 10
Number of Columns: 0

+6
source share
3 answers

This is an integer division problem!

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

it should be

 int column = 7 / 10; 

and in integer divisions 7/10 7/10==0 .

What you want to do is split the length of one line, for example. sizeof(result[0]) size of one element of this row, for example. sizeof(result[0][0]) :

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

It is 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; } 
+6
source

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; } 
+5
source

All Articles