I got the problem with this code..
can anyone explain me the output..


main()
{
int i = 258; int *iPtr = &i;
printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );}

Maybe it would help to look at what 258 is in binary

00000001 00000010

Which is 1 and 2.

As char pointer takes only 8 bit .

main()
{
int i = 258; int *iPtr = &i;
printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );
}

In this program the bit orientation is 00000001 00000010.
First char pointer

(char*)iPtr

point a address where the data is 00000010
and next address is containing 00000001.
So we are getting output as
2 1.

Yup as told by gerard4143 and asit mahato.
Saurav:-Don't get confused by seeing negative sign as output.

for eg:--

main()
{
int i = 449; int *iPtr = &i;
printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );}

output is -> -63 1.

As binary of 449 is 00000001 11000001

for 11000001, most significant bit(MSB) is 1 which is sign bit if there is 0 means number is positive and if there is 1 i.e, number is negative here it is 1 means number is negative. so converts this binary into its 2's complement which is 00111111(63 in decimal but -(minus) sign will be there because of 1 in MSB so -63).
for 00000001 MSB is 0 so number is positive and nothing to worry.
now final binary form is:--
00000001 00111111
= 1 -63 (in decimal)
so output will be -63 and 1.

@cse.avinash
actually i didnt get the meaning of *((char*)iPtr)..what does it mean??

@cse.avinash
actually i didnt get the meaning of *((char*)iPtr)..what does it mean??

It means cast iPtr to a character pointer with (char*) and then dereference it.

*((char*)iPtr) getting the value it points to.

its kind of big endian and little endian thing , means-
>These are d terms dat describe the order in which a sequence of bytes are stored in computer memory.

>Little Endian means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address. That is, the little end comes first.

For example, a 4 byte, 32-bit integer
s

Byte3 Byte2 Byte1 Byte0


will be arranged in memory as follows:


Base_Address+0 Byte0
Base_Address+1 Byte1
Base_Address+2 Byte2
Base_Address+3 Byte3

Example :Intel processors use "Little Endian" byte order.

>"Big Endian" means that the higher order byte of the number is stored in memory at the lowest address, and the lower order byte at the highest address. The big end comes first.


Base_Address+0 Byte3
Base_Address+1 Byte2
Base_Address+2 Byte1
Base_Address+3 Byte0

Motorola, Solaris processors use "Big Endian" byte order.

google it for more information..

Now whats happening here iPtr is a pointer storing integer address.
iPtr will represent the same address as represented by casting it with char i.e, (char*)iPtr.
but here we are limiting the value at that address to 1 byte instead of sizeof(int) byte by casting it, and we are returning the value as an int not as a char.
so 1 byte= 8 bits. and that binary conversion , the little endium things (as intel processor) are taken into consideration for value at address of (char*)iPtr and next bit for (char*)iptr+1.


:icon_idea: MUST GO THROUGH THE LINK FOR YOUR ALL DOUBTS ABOUT POINTERS, MEMORY, INPUT NUMBERS AND DATA CHARACTERS..

thanx cse.avinash and gerard for helping me out..

nd avinash thnx for the link..

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.