ZZ Coder ZZ Coder - 4 months ago 13
C Question

How to Find Dimension of a Multiple Dimension Array in C

I declared a 2-dimensional array like this:

char *array[][3] = {
{"a", "b", "c"},
{"d", "e", "f"},
{"u", "v", "w"},
{"x", "y", "z"}};


How do I find out the first dimension?

Answer

sizeof array / sizeof array[0]

BTW, please don't worry about the lack of function-like parens following sizeof. It's really an operator; those parens have always been optional. I did test this expression with the following program:

printf("%zd\n", sizeof array / sizeof array[0]);

(Use %d for C89.)