curious_one said:
All,
I have a struct
struct {
char a;
char b;
}some_struct;
I have a shared memory that can contain 16bit wide data, I find that
when writing an 8bit value in to char "a" the same value is
over-written onto "b".
*pSome_struct->a = 5; both a & b are 5. But if I write it
as *(unsigned short*)&pSome_struct->a = 5 << 8; the value is not
over-written into b Any clue as to why this happens ?
First, "shared memory" is unclear. Do you mean a piece
of memory that is accessible both to your program and to some
other program? If so, the other program may be doing things
to it without your program's knowledge -- and any further
questions along that line should be directed to some other
newsgroups, because C has no notion of or support for "other
programs."
Second, how do you "find" that `b' changes? How do you
know it didn't hold a 5 before you stored into `a', and how
did you determine its value afterward?
Third, what does `pSome_struct' point to? Don't say
"to a `struct some_struct'," explain exactly which struct
it points to and how that struct was created.
Fourth, the subterfuge with the pointer cast produces
undefined behavior as far as the C language is concerned.
Your machine may define a useful behavior for this construct,
but the C language does not. Any further pursuit of that part
of your inquiry belongs in a forum devoted to your machine,
not in a forum about the C language.
Fifth -- ah, the heck with it. You've told the doctor
that you're experiencing joint pain, but since you haven't
revealed whether it's in your ankle, your elbow, or your
hand-rolled illegal cigarette you can't expect much in the
way of a diagnosis. Provide a short, complete, compilable
example that exhibits your problem, and maybe somebody will
be able to do something about it.