"Dharma" <dh**********@huawei.com> writes:
struct a
{
byte d;
byte buf[16];
}text;
C has no standard type called "byte".
text.buf[0] = 'A'
text.buf[1] = '\0'
text.buf[2] = 'B'
text.buf[3] = '\0'
text.buf[4] = '\0'
text.buf[5] = '\0'
main()
This should be "int main(void)".
{
unsigned short *c = NULL;
c = (unsigned short*)&text.buf;
if(0x0000 == *c)
{
return ;
}
}
i have a structure as above. Iam type casting the byte to unsigned
short pointer.
No, you're converting an address of (i.e., a pointer to) a byte to a
pointer to an unsigned short.
when i do (0x0000 == *c) the first byte becomes 0x00. I found that buf
starts with odd memory address, If i make the memory Alignment to even
there is no problem,
What the low level does if memory Alignment is odd aligned and it is
unsigned short* type.
Alignment requirements vary from one system to another. If type short
requires even alignment, and you have a misaligned pointer, attempting
to dereference it (as in *c) invokes undefined behavior. As far as
the language is concerned, anything could happen.
The solution: don't do that.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.