0

how the sizeof array is the total number of elements multiped by its base data type size, when the name of the array gives only the base address.

int array [5];
       sizeof array ;

gives 20 bytes.

why not 4 bytes.

because

array = &array[0]
.
4
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Narue
0

>>why not 4 bytes.

You answered your own question in the first sentence you posted. Because 5 * sizeof(int) = 5 * 4 = 20. Simple grade-school math.

0

>>why not 4 bytes.

You answered your own question in the first sentence you posted. Because 5 * sizeof(int) = 5 * 4 = 20. Simple grade-school math.

why sizeof does not consider name of the array as some address and give pointer size bytes.

0

>why sizeof does not consider name of the array
>as some address and give pointer size bytes.

Because the conversion from array to pointer only occurs in a value context. The operand of sizeof is being used in an object context, so the conversion doesn't happen.

There are three object contexts for an array:

  1. As the operand to sizeof ( sizeof a ).
  2. As the operand to address-of ( &a ).
  3. As a string literal initializer ( char a[] = "booga"; )

In all other cases the name of an array is converted to a pointer to the first element.

Edited by Narue: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.