+ 1

sizeof C++

int numbers[100]; cout << sizeof(numbers) / sizeof(numbers[0]); // Outputs 100 Why is it 'sizeof(numbers[0])' and not 'sizeof(numbers[1])' ?

8th Apr 2019, 12:24 PM
Msizi
2 Respostas
+ 11
numbers[0] and numbers[1] are both 1 slot in the integer array. Both are of the same size, so it doesn't matter what you choose.
8th Apr 2019, 1:02 PM
Hatsy Rei
Hatsy Rei - avatar
+ 10
Msizi, Size of integer variable is 4 byte by default. And because of that sizeof(numbers) i. e. the Whole Array Give 400 as there are 100 integers in array each taking 4 bytes of memory 100*4 = 400. Now sizeof(numbers[0]) is only 4 bytes because it is only One element of the Array. So, 400/4 gives you 100 in Output. So output is came out as 100.
8th Apr 2019, 12:35 PM
GAWEN STEASY
GAWEN STEASY - avatar