If I have an array declared like this:
int a[3][2];
then why is:
sizeof(a+0) == 8
whereas:
sizeof(a) == 24
I don’t understand how adding 0 to the pointer changes the sizeof output. Is there maybe some implicit type cast?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you add
0toa, thenais first converted to a pointer value of typeint(*)[2](pointing to the first element of an array of typeint[3][2]). Then0is added to that, which adds0 * sizeof(int[2])bytes to the address represented by that pointer value. Since that multiplication yields 0, it will yield the same pointer value. Since it is a pointer,sizeof(a+0)yields the size of a pointer, which is 8 bytes on your box.If you do
sizeof(a), there is no reason for the compiler to convertato a pointer value (that makes only sense if you want to index elements or to do pointer arithmetic involving the address of the elements). So expressionastays being of an array type, and you get the size ofint[3][2]instead the size ofint(*)[2]. So,3 * 2 * sizeof(int)which on your box is 24 bytes.Hope this clarifies things.