On Thu, 10 Jun 2004 18:08:37 -0700, tweak <bw********@cox.net> wrote
in comp.lang.c:
First your title displays a fundamental misunderstanding that is
augmenting your confusion.
You _are not_ casting structures, and you _cannot_ cast structures in
C.
You can cast a pointer to any type of object to a pointer to any other
type of object at all, and that is what you are doing here.
I understand the text just fine. It's when I assign the pointer to
another pointer of the type being cast to and look at the dereferenced
values that I get confused.
As far as the C standard is concerned, there is no requirement that
dereferencing such a pointer has defined behavior at all.
If I declare the structure
struct sockaddr_in servaddr
Not a standard C data type.
and I put in
servaddr.sin_family = AF_INET; /* which corresponds to a value */
and I put in
servaddr.sin_port = htons(13) /* which converts port 13 and assigns it
to sin_port */
Not a standard C function.
[snip]
I hope that clarifies. I am okay with setting up clients and servers;
I'm just stuck on how C is working above.
As far as C is concerned, when you play type punning you are
responsible for knowing what you are doing, with any sort of undefined
behavior as a consequence. If the members of the two structure types
are different there is a good chance that when accessing some of the
members you will be accessing objects with different lvalues than the
objects were written with. In that case, C is not working at all,
this produces undefined behavior in which case the C standard makes no
guarantees or requirements at all about what will happen.
Exactly what might or might not happen on your implementation, if it
is predictable at all, depends on a combination of at least three
things.
One of these is the definition of the data types, and neither of these
structure types are defined by standard C. They are non-standard
extensions provided by your compiler/OS, and so off-topic here.
The second factor is the underlying hardware architecture of your
platform, which is also not defined by the C standard and is off-topic
here.
The final factor is your particular compiler, and how it chooses to
act in this instance. Specific compilers are also off-topic here.
So your question is about how your specific compiler reacts when you
invoke undefined behavior by punning two non-standard structure types.
There is not one single aspect of this question that is NOT off-topic
here.
You need to ask this question in a group that supports your particular
compiler/OS combination, such as news:comp.os.linux.development.apps
or news:comp.unix.programmer. It is just plain not a C language
question at all.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html