ZZ Coder ZZ Coder - 1 year ago 126
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 Source

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.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download