views:

302

answers:

2

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?

+4  A: 

Why don't you give:

sizeof(array) / sizeof(char*) / 3

a shot?

This is assuming you know the data type (char*) and other dimension (3).

You divide the size of the structure by the size of each element to get the total number of elements, then divide it by the first dimension, giving you the second.

Aside: my original answer used 'sizeof("a")' which was, of course, wrong, since it was the size of the array (2, being 'a' and '\0'), not the pointer (4 on my hideously outdated 32-bit machine). But I love the way I got two upvotes for that wrong answer - don't you bods actually check the answers here before voting? :-)

paxdiablo
Not a very elegant solution - compared with DigitalRoss's solution. You have to know quite a bit about the type to write that. And yes, people do up-vote without always checking the answer - or spotting the problem with the answer.
Jonathan Leffler
+7  A: 

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 btw for C89.)

DigitalRoss
If you use %d for C89, you should also use a cast to int.
Jonathan Leffler
True, or use %ld and cast to long. I've actually made that change a zillion times to squelch warnings on 64-bit systems. Should have mentioned it.
DigitalRoss
Parens around the operand of `sizeof` are not necessary with expressions (the parens in that case are simply more of the expression). However, they are required if the operand to the sizeof operator is a type.
Michael Burr
Also - even when using an expression as the operand to `sizeof`, I persnoally like parens since then I don't have to worry about remembering the precedence of the sizeof operator. I have a hard time remembering more than "multiplication and division come before addition and subtraction".
Michael Burr
Actually, the parens are part of what makes a type expression, not some requirement of `sizeof`. When people use `sizeof(int)` that's typically the one place where they do use `sizeof` as an operator, albeit without typically realizing it.
DigitalRoss
@DigitalRoss - (int) is not an expression. The requirement of parens around a type operand to `sizeof` is because the standard says so: 6.5.3.4/2 of C99 (The sizeof operator), "The sizeof operator yields the size (in bytes) of its operand, which may be anexpression or the parenthesized name of a type."
Michael Burr
I know, and I agree, it certainly does look like an arbitrary requirement of `sizeof` the way its written up, but the underlying reason is the same reason that casts have parens, the grammar is too ambiguous to easily parse a "type expression" without them.
DigitalRoss