nevzatseferoglu
nevzatseferoglu

Reputation: 1186

inexplicable change in value while using pointer typecasting

#include <stdio.h>
int main( void )
{ 
    int num = 1;
    char *b;

    b = (char*) &num;
    *(++b) = 2;
    printf("%d\n",num);

    return 0; 
}

Explanation : When I compiled this code , I encountered "513" as an output.When I use a comment line for that line: `*(++b)=2;

Output converts into "1".

Question 1: Why did I encounter "513" as an output ?

Question 1: Why did output change when use comment line that I implied ?

Upvotes: 2

Views: 82

Answers (4)

Achal
Achal

Reputation: 11921

This

int num=1;

represented in 32 bit system as below

     0x103      0x102       0x101       0x100  (let's assume base address of num 0x100)
  -----------------------------------------------
 | 0000 0000 | 0000 0000 | 0000 0000 | 0000 0001 |
  -----------------------------------------------
                                                num
MSB                                          <--LSB

And here

char *b; /* char pointer i.e at a time points to 1 byte */
b=(char*)&num; /* b points to &num i.e 0x100 in above diagram */

it looks like

    0x103      0x102       0x101       0x100  
  -----------------------------------------------
 | 0000 0000 | 0000 0000 | 0000 0000 | 0000 0001 |
  -----------------------------------------------
                                                num
                                                 b <-- b points here 

Now when this

*(++b)=2;

gets executed, first ++b happens that means char pointer b gets incremented by one byte i.e it points to 0x101 location and then content of only 0x101 location assigned by 2. It looks like

     0x103      0x102       0x101       0x100  
  -----------------------------------------------
 | 0000 0000 | 0000 0000 | 0000 0010 | 0000 0001 |
  -----------------------------------------------
                                     |          num
                                     b <-- b points to 0x101  

Now when you print num it prints 512 + 1 which is 513. I hope it clears your doubt.

Upvotes: 0

H.S.
H.S.

Reputation: 12669

When I compiled this code , I encountered "513" as an output.

You are getting this output because of this statement:

*(++b)=2;

Initially, b is pointing to num. Statement *(++b)=2 will first increment the pointer b and point to next byte and then dereferencing it and assigning 2.
Assuming on your platform int is 32 bit then:

initially
num = 1

00000000 00000000 00000000 00000001


num after this statement
*(++b)=2;

00000000 00000000 00000010 00000001

which is the binary representation of `513`.

When I use a comment line for that line: *(++b)=2; Output converts into "1".

Of course, you have initialized num with 1 and what else you expect when printing num without making any change to it.

Upvotes: 0

dbush
dbush

Reputation: 223927

Assuming that an int is 32 bit with little endian byte ordering on your system, the representation of num is 0x00000001 and looks like this in memory:

-----------------
| 1 | 0 | 0 | 0 |
-----------------

Then you point b to num:

  b
  |
  v
-----------------
| 1 | 0 | 0 | 0 |
-----------------

Then you do *(++b)=2;, it increments b, dereferences the incremented pointer, and writes 2 to that location. So you now have.

      b
      |
      v
-----------------
| 1 | 2 | 0 | 0 |
-----------------

So now the representation of num is 0x00000201` which is 513 in decimal.

Upvotes: 1

Tanveer Badar
Tanveer Badar

Reputation: 5523

It appears that on your machine, chars are byte-sized. That is why you are seeing this completely normal behavior.

Your num variable looks like this in memory.

0x00000001

You take address of it, treat it as a char* then set second char to 2 so now your num becomes.

0x00000201

When you convert that back to decimal, it correctly outputs 513.

Upvotes: 0

Related Questions