``````int i=512;
char *c=(char *)&i;
c[0]=2;
printf("%d",i);
``````

o/p is 514

4
Contributors
15
Replies
20
Views
5 Years
Discussion Span
Last Post by I_m_rude
Featured Replies
• 2

The 2 byte bit pattern of 512 is **0000 0010 0000 0000**. The 2 byte bit pattern of 514 is **0000 0010 0000 0010**. The one byte bit pattern of 2 is **0000 0010**. You do the math.

• 1

> Can you tell me one thing that c is just a pointer, then how is it treated like an array ? ...seriously? We've been over this so many times over the course of a few months now, I'd expect you to be a pointer guru at this point. :P …

• 1

> Firstly, We haven't discuss these type of things at any time. Clearly you don't remember, but I do because I spent a lot of time explaining to you how arrays and pointers work. And I'm quite sure that I described the relationship both ways. > Secondly, You have used …

The 2 byte bit pattern of 512 is 0000 0010 0000 0000. The 2 byte bit pattern of 514 is 0000 0010 0000 0010. The one byte bit pattern of 2 is 0000 0010. You do the math.

why c[0] is adding to 512?

512 in binary is 10 0000 0000
The int is probably 2 bytes (guessing by your name you're using Turbo C)
So i contains (0000 0010)(0000 0000)
typecasting i to the character c, c[0] is (0000 0000), c[1] is (0000 0010)
Replace c[0] with (0000 0010) makes i (0000 0010)(0000 0010) which is 514.

Try outputting c[0] and c[1] and you'd be able to see what is happening. That is if you can do the binary conversion.

hey, James sir, Can you tell me one thing that c is just a pointer, then how is it treated like an array ? :-0 oh. please explain this. thanks in advance to you.

Can you tell me one thing that c is just a pointer, then how is it treated like an array ?

...seriously?

We've been over this so many times over the course of a few months now, I'd expect you to be a pointer guru at this point. :P

A pointer is not an array, but array indexing essentially becomes the dereference of an offset from a pointer. `a[i]` is equivalent to `*(a + i)`, so it doesn't matter if `a` is a pointer or an array, you can use indexing syntax with both.

Firstly, We haven't discuss these type of things at any time. we have discussed issues of array to pointers but i never expected that vice-versa is also true.

Secondly, You have used a hindi word "guru" ;) you are aware of hindi also ?

thirdly, james sir, so what c[0],c[1],c[2] and so on are representing here ?

Fourthly, I am near to level of guru as in my university, No body has so strong concepts of C pointers till date. althoguh, i am not so so so good, but am better than many . it is result of your effort, I can confess this at any time. thanks.

Firstly, We haven't discuss these type of things at any time.

Clearly you don't remember, but I do because I spent a lot of time explaining to you how arrays and pointers work. And I'm quite sure that I described the relationship both ways.

Secondly, You have used a hindi word "guru" ;) you are aware of hindi also ?

I'm aware of hindi, but "guru" is common in English too.

so what c[0],c[1],c[2] and so on are representing here ?

The bytes of the integer. Do some research on a concept called type punning. That's what's being done here.

oh my god... Yes! we have discussed on that. And i have seen the reason why didn't i remember that. the reason is that at that time, I was damn more concerned about ARRAYS particularly. sir, Seriously sorry, although I have read all the things you have given, but due to lack of focus on that thing make me to forget that. really sorry for that thing. am upset. :(

sir, I have searched many links. is there any difference between type casting and type punning ? please tell me. thanks in advance to you james sir.

The strict definition is probably close to the same for both, but I see type punning as breaking the type system to view component parts of an object. In this case the integer is being punned into an "array" of bytes that make up the integer.

but why is it punned to char only ? can't it be used in int only ?

secodnly, please give me good links to type punning. I have learnt many things about punning but i want more. thanks

but why is it punned to char only ? can't it be used in int only ?

It's punned into a pointer to char. A char is equivalent to a byte, so a pointer to a byte (or an "array" of bytes if you prefer) is the appropriate way to access each individual byte in a multibyte object without using bitwise operations.

I don't use casting in this case, rather I'm using the `_real4_t` and `_real8_t` unions. You can search for those type names to see all of the places they're used.