Understanding the & operator

What I know about the '&' operator, it returns the base address of the operand in memory.

Imagine the following scenario (as on my machine):

  • sizeof (int) = 4 bytes
  • sizeof (float) = 4 bytes
  • sizeof (char) = 1 byte

Now, if I write something like this:

void main() {
 int i = 5411;
 int *ip = &i;
 char *c = &i;

 printf("%d",*ip);
 printf("%c",*c);
}

The first printf () should give me 5411. Speaking of the second printf (), the base address I contain is 10101001 (the most significant bit is 8 bits = 1 byte for a pointer of type char). Therefore, * c should give me 169, which when converted to% c is an invalid character.

But the compiler gives me "#" or some other valid output. Why is this so? Any inputs?

EDIT (taken from author's comment on one of the answers):

, .
: = 5411

+5
5

, , . 5411 .

5411 = 1010100100011

13 , , int - 32-, 32

5411 = 00000000 00000000 00010101 00100011

(x86, ARM ) , :

00100011   00010101    00000000    00000000
^
c          c + 1       c + 2       c + 3
ip

, *c 00100011, .. 35 ('#').

+24

ASCII 127. , , , , *c, %d...

 printf("%d",*c);

... , .

+5

-, . C, ++ char * int *. c .

-, i - - " ", . , , *c ( 130 8- char, 114). , , *c ( 0 8- char). , 130 0 %c.

-, , " ". - . , # . , ?

+3

* c i, c i. ( ) .

+1

, - ,

printf("0x%X, %X|%X|%X|%X\n", 
  i, 
  i & 0xFF,
  (i >> 8) & 0xFF
  (i >> 16) & 0xFF
  (i >> 24) & 0xFF
  );

c[0], c[1] .. %c.

+1

All Articles