I'm not sure why this is happening, I think I'm doing everything correctly.. Maybe someone can help point me in the right direction.
unsigned short* x;
int textLeft[16];
x = shm->textLeft;
These are spaced out in the program so I didn't want to copy a bunch of code but if more is needed please let me know.
Shouldn't this work correctly without giving me the incompatible pointer type?
No, this should not work, because you're assigning an int*
value to an unsigned short*
variable, which causes undefined behavior per the C strict aliasing rule .
The way to make this work without changing the types is to
x = (unsigned short *)(shm->textLeft);
, and -fno-strict-aliasing
to turn the aliasing rule off. But really, I strongly recommend you change the types to be compatible, since otherwise you're tying yourself to a single compiler's extensions to the C standard.
`unsigned short`
is not
`int`
So define
x
as int *
or textLeft[16]
as unsigned short
and things are ok.
In your case x
is a unsigned short pointer and textLeft
is a signed integer. You are trying to assign signed integer address to unsigned short pointer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.