+ 1

Some one can explain me this ? Why 5 ?

What is the output of this code ? (Int is 4 bytes) Int x[5]; Int y = sizeof(x) / 4; Printf("%i",y); The answer is 5

21st Jul 2021, 6:46 PM
Roma Didyk
1 Answer
+ 9
An array is a contiguous memory block, therefore x is pointer to that memory block. Having x[5] means reserving 20 bytes of memory on the stack ie 4 bytes each element(integer) in the array. sizeof(x) returns 20 which when divided by 4 returns 5.
21st Jul 2021, 6:52 PM
Kirabo Ibrahim
Kirabo Ibrahim - avatar