What does this mean?

Hello, can you tell me what does this mean
1
2
int A[] = { 66,55,444,33,22,111};
n = sizeof(A)/sizeof(int);

And what's sizeof(int)?
It is a definition of an array and initialization of it with values in braces.
sizeof( int ) is the size in bytes of an object of type int.
Last edited on
So basically
n = 6/ 32767?
Last edited on
no. Sizeof returns the size in bytes.

So assuming an int is 4 bytes:

sizeof(A) == 24 (array of 6 ints, each 4 bytes)
sizeof(int) == 4 (size of a single int)

sizeof(A) / sizeof(int) == 6 (the number of elements in the array).


Though this is a poor way to determine the array size because it is error prone (it will not work as expected if 'A' is a pointer).
n = sizeof(A)/sizeof(int);

This will give you the total elements in the array.

sizeof( A ) - Total elements of( typically ) 4 bytes.
sizeof(int); - Again, typically 4 bytes for an int.

I say 'typically' because it can vary from computer to computer.

So, 5 elements of type int would be 5 * 4 = 20 bytes.

So the above code:
n = elements * 4 bytes / type int( 4 bytes ) = total elements in the array.

Hope you understand that. (:

Faster at typing, Disch? lol.
Last edited on
Please open at last some book on C++ and read it. It is not a forum for those who is even lazy to open a book.
Thank you all.
Topic archived. No new replies allowed.