Search This Blog

Tuesday, October 5, 2010

typecasting-several situations!!

Typecasting,when dealt alone,is not a difficult concept to understand and imbibe.However,confusions surface when we encounter a code in which a few different concepts of C are mixed up.Also our attempts, to arrive at intended results,go awry when we use typecasting in various situations without a clear understanding or due to intangible mistakes.I am describing a few here which i have frequently encountered.

consider the following piece of code and try to comprehend the output they produce...

int a[]={10,20,30,40,308};
char *p=(int*)a;

*pointer is of char type hence at most it can de-reference 1 byte of the *integer type element of the array.10 is at the base address and can be *represented well in 8 bits as 0000 1020 (as a four byte integer it would be FFFA *or 0000 0000 0000 0000 0000 0000 0000 1020).Now on a little endian machine, the *pointer reads from *lower address.Hence here in this case character pointer 'p' *can read and accommodate well the value 10.


*What happens here is a bit hazy at the first look.Lets analyze the operations on *the pointer and see where they take us.Pointer p is of char type hence when we *add 1 to it,it would move to the next one byte chunk in the memory.It was pointing *at the first byte of the base address in beginning so here it moves to the next *byte(not to the next element in the array,which is conclusion at first sight),that *happens to be 0000 0000(refer to the bit representation of first element in *previous explanation).Hence the output 0.


*this justifies the above explanation.When we move the char pointer by 4 bytes we
*are moving to the first or lower byte of second element of an int array.As int
*occupies 4 byte.


/*This has to do with the working of printf function. Output would be undefined *here since two operations are carried out on same operand P in argument list of *printf function.Hence it may not print value at the lower and next higher byte
*of the base address.

p = (int*)p + 4;

*P a a little attention here.We have typcasted p to an int pointer and added 4 to *it on the right hand side of the expression and assigned this value to the p,which *is a char pointer(remember its declared so,typecasting it to integer does not *change its fundamental behavior).However again in printf we have typecasted it to *int pointer.Hence it prints value represented by four bytes at the 4th index(i.e. 5th element)which is 308(308:0000 0000 0000 0000 0000 0001 0011 0100).Had it not been typecasted in pritnf again result would have been as below.


*After the operations in the previous step.pointer is pointing to the lower byte of *fifth element but since it is char pointer by nature,it can read only one byte *from where it is pointing.And that is equivalent to 52(refer the binary *representation of 308 above)


Even though it does not cover all that could be done with typecasting,this article provides an insight to look at cases little deeper.After all C is the language where you don't learn new things until you screw it up somewhere!

No comments:

Post a Comment