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]
.

Recommended Answers

All 4 Replies

>>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 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.

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.