Why does sizeof(argv)/sizeof(argv[0]) give me the size of an array in C++? -


if have array argument in main

int main(int argc, char* argv[]) 

why

sizeof(argv)/sizeof(argv[0]) 

always reliably give me length of array?

it doesn't.

i got bit carried away when writing answer; it's of language-lawyer approach simple question. i'll add quick summary should enough answer question. pedantic , overly verbose version of answer below horizontal line.

given:

int main(int argc, char* argv[]) 

argv isn't array @ all; it's pointer. (c , c++ don't permit parameters of array type; looks array parameter pointer parameter.) argv point (at run time) first element of array, there's no information in declaration how big array is. that's why need argc parameter provide information.

so sizeof(argv)/sizeof(argv[0]) doesn't give number of elements in array; divides size of pointer size of pointer, probably gives 1. (why "probably"? that's part of overly pedantic answer below.)

now if define explicitly array object:

int an_array[42]; 

you can use idiom compute number of elements in array:

sizeof an_array / sizeof an_array[0] 

this yields 42, number of elements in array. way works (i think) straightforward: it's size in bytes of whole array divided size in bytes of 1 of elements.

but works only actual array, not argv looks array pointer.

the relationship between arrays , pointers can confusing. section 6 of comp.lang.c faq explains well, , or of applies both c , c++.

now long-winded pedantic explanation many digressions:


argv pointer, pointer pointer char. parameter declaration (and only in context), char *argv[] equivalent char **argv.

sizeof (argv) number of bytes in char** pointer.

sizeof (argv[0]) number of bytes in char* pointer.

sizeof (argv) / sizeof (argv[0]) 1 (assuming char* , char** have same size, in implementations).

now that's been defined array object:

some_type an_array[count]; 

that expression work; this:

sizeof an_array / sizeof an_array[0] 

does give number of elements in an_array, namely count. reason should, think, obvious. number of bytes in array number of bytes in 1 element of array multiplied number of elements. so:

sizeof an_array == sizeof an_array[0] * count 

and, rearranging terms:

sizeof an_array / sizeof an_array[0] == count 

incidentally, sizeof an_array[0] can written sizeof *an_array, due way indexing operator defined in c.

(note sizeof operator doesn't require parentheses around argument, if argument expression such object name. operand sizeof either expression or parenthesized type name. if prefer use parentheses sizeof, can that.)

this common idiom computing number of elements in array -- works if have name of array itself, not pointer first element.

[the following applies c. believe applies c++ (i didn't notice question tagged c++, not c).]

in answer question raised in comments, no, char* , char** not required have same size. c standard's requirements on pointer representation are:

a pointer void shall have same representation , alignment requirements pointer character type. similarly, pointers qualified or unqualified versions of compatible types shall have same representation , alignment requirements. pointers structure types shall have same representation , alignment requirements each other. pointers union types shall have same representation , alignment requirements each other. pointers other types need not have same representation or alignment requirements.

reference: n1570, 6.2.5p28.

the c++ standard has @ least some of this; section 3.9.2 [basic.compound] of n3485 draft says:

an object of type cv void* shall have same representation , alignment requirements cv char*.

i haven't found corresponding text rest of quoted c standard.

on word-addressed machine, char* pointer plausibly require more information specify both word , byte within word needed char** pointer, needs specify aligned word.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -