> -----Original Message-----
> From: vishal.jain [mailto:vishal.jain@globaledgesoft.com]
> Sent: Friday, April 01, 2005 12:26 PM
> To: Vijay Zanvar (WT01 - TELECOM SOLUTIONS)
> Subject: Any answer??
>
>
>
>
> The code is as follows..
>
> 1 main( )
> 2 {
> 3 char a[ ] = "XYZABC";
> 4 char *b = "XYZABC";
> 5 printf("\n %d %d ", sizeof(a), sizeof(b) );
> 6 printf("\n %d %d ", sizeof((*a)),sizeof(*b));
> 7 }
>
> The output is
>
> 7 4
> 1 1
>
> That seems pretty obvious ..
>
OK.
> If in line no 6 i make it to
> 6 printf("\n %d %d ", sizeof((*a + 1 )),sizeof(*b + 1
> ));
>
> The output becomes
> 7 4
> 4 4
>
> Why ???
> Why the size of ( *a + 1 ) and ( *b + 1 ) is 4 ??
>
This is because of type promotion. When an operanad is of type char and the
other is of int, char is promoted to int. So, ultimately the expression becomes
sizeof (int), which is 4.
Read K&R section 2.7. It says:
"When an operator has operands of different types, they are
converted to a common type according to a small number of
rules. In general, the only automatic conversions are those
that convert a ``narrower'' operand into a ``wider'' one
without losing information, such as converting an integer
into floating point in an expression like f + i. ..."
Vijay
               (
geocities.com/vijoeyz/faq)                   (
geocities.com/vijoeyz)