0
#include <stdio.h>

int main(){
    char a[10]={0,1,0,1,0,1,0,1};
    unsigned short *p;
    p=(unsigned short *)&a[0];
    *p=1024;
    printf("%d",a[1]);
    return 0;
}

Why answer is 4?? Isn't 1024 entered in array a[0] and a[1] remains? Why does it affect up to a[1]?

Barmar
  • 741,623
  • 53
  • 500
  • 612
somi
  • 1
  • 1
    it's undefined behavior. C forbids accessing `char` via `short` – tstanisl Oct 20 '22 at 16:30
  • The result depends on whether the CPU is big-endian or little-endian. – Barmar Oct 20 '22 at 16:37
  • The posted code [violates strict aliasing and invokes undefined behavior](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule). It can also invoke undefined behavior [if any alignment restrictions are not met](http://port70.net/~nsz/c/c11/n1570.html#6.3.2.3p7). Merely creating the pointer is enough to invoke undefined behavior. – Andrew Henle Oct 30 '22 at 17:44

2 Answers2

0

i tested my self and ,if you test the value of a[0] you will see it never change the pointer is changing the value of a[1] but the @ in p is the @ of a[0]. interesting behavior and the answer is that using char, for char max value is 125 if i m not wrong so if you providing a huge value will overwrite the next value and this is interring

  • Signed char max is 127. Unsigned char max is 255. – Barmar Oct 20 '22 at 16:40
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 23 '22 at 17:49
0

I'm reasoning on a big endian system (most significant byte stored at the smallest address).

char a[10]={0,1,0,1,0,1,0,1};

In binary, your array initially looks like that in your memory : 0000.0000 0000.0001 0000.0000 0000.0001 0000.0000 0000.0001 0000.0000 0000.0001 0000.0000 0000.0000

A char only takes a byte in memory. That means, from right to left, the first 0000.0000 is a[0], then 0000.0001 is a[1], etc.

unsigned short *p;
  p=(unsigned short *)&a[0];
  *p=1024;

You assigned to p the address of the array. Then you dereferenced it and put in the address stored by p an unsigned short equal to 1024. In binary, 1024 looks like : 0000.0100.0000.0000 An unsigned short takes two bytes in memory.

So,this is what your array becomes after your modification : 0000.0100.0000.0000 0000.0000 0000.0001 0000.0000 0000.0001 0000.0000 0000.0001 0000.0000 0000.0000

==> What happened is, since you treated your p as a pointer to an unsigned int, you changed the first 2 bytes instead of just the first one (if it was a char). Then, when you want to access your data via your char array, it will be treated as a char, byte by byte. Then, 0000.0100 which is 4 in decimal is a[0] and 0000.0000 which is 0 in decimal is a[1].

We can deduce that your system is little endian since you instead got 4 for a[1] and 0 for a[0] (I'll let you find more explanations about endianness on google)

Arkpsss
  • 1
  • 1