📄 c-c+语言struct深层探索 - k的专栏 - csdnblog.htm
字号:
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 36pt; mso-char-indent-count: 3.0"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">case PACKET_A: </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 36pt; mso-char-indent-count: 3.0"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">…<SPAN style="mso-spacerun: yes">
</SPAN>//A</FONT></SPAN><SPAN
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 auto">类报文处理</SPAN><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto">
</SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 36pt; mso-char-indent-count: 3.0"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">break; </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 36pt; mso-char-indent-count: 3.0"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">case PACKET_B: </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 36pt; mso-char-indent-count: 3.0"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">…</FONT></SPAN><SPAN
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 auto"> </SPAN><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman"><SPAN style="mso-spacerun: yes">
</SPAN>//B</FONT></SPAN><SPAN
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 auto">类报文处理</SPAN><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto">
</SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 36pt; mso-char-indent-count: 3.0"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">break; </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 36pt; mso-char-indent-count: 3.0"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">case PACKET_C: </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 36pt; mso-char-indent-count: 3.0"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">…<SPAN style="mso-spacerun: yes">
</SPAN>//C</FONT></SPAN><SPAN
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 auto">类报文处理</SPAN><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto">
</SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 36pt; mso-char-indent-count: 3.0"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">break; </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18pt; mso-char-indent-count: 1.5"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">} </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18pt; mso-char-indent-count: 1.5"><SPAN
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 auto">以上程序中最值得注意的是</SPAN><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto">
</SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18pt; mso-char-indent-count: 1.5"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">Send( (char *)&sendCommuPacket ,
sizeof(CommuPacket) ); </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18pt; mso-char-indent-count: 1.5"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">Recv( (char *)&recvCommuPacket ,
sizeof(CommuPacket) ); </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18pt; mso-char-indent-count: 1.5"><SPAN
style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的强制类型转换:</SPAN><SPAN
lang=EN-US style="FONT-SIZE: 12pt"><FONT face="Times New Roman">(char
*)&sendCommuPacket</FONT></SPAN><SPAN
style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN
lang=EN-US style="FONT-SIZE: 12pt"><FONT face="Times New Roman">(char
*)&recvCommuPacket</FONT></SPAN><SPAN
style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,先取地址,再转化为</SPAN><SPAN
lang=EN-US style="FONT-SIZE: 12pt"><FONT
face="Times New Roman">char</FONT></SPAN><SPAN
style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">型指针,这样就可以直接利用处理字节流的函数。</SPAN><SPAN
lang=EN-US style="FONT-SIZE: 12pt"> </SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18pt; mso-char-indent-count: 1.5"><SPAN
style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">利用这种强制类型转化,我们还可以方便程序的编写,例如要对</SPAN><SPAN
lang=EN-US style="FONT-SIZE: 12pt"><FONT
face="Times New Roman">sendCommuPacket</FONT></SPAN><SPAN
style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所处内存初始化为</SPAN><SPAN
lang=EN-US style="FONT-SIZE: 12pt"><FONT
face="Times New Roman">0</FONT></SPAN><SPAN
style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,可以这样调用标准库函数</SPAN><SPAN
lang=EN-US style="FONT-SIZE: 12pt"><FONT
face="Times New Roman">memset()</FONT></SPAN><SPAN
style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:</SPAN><SPAN
lang=EN-US style="FONT-SIZE: 12pt"> </SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18pt; mso-char-indent-count: 1.5"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">memset((char *)&sendCommuPacket,0,
sizeof(CommuPacket)); </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt"><FONT
face="Times New Roman"><STRONG style="mso-bidi-font-weight: normal"><SPAN
lang=EN-US
style="FONT-SIZE: 14pt; mso-fareast-font-family: 'Times New Roman'"><SPAN
style="mso-list: Ignore">2.<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN></STRONG><STRONG
style="mso-bidi-font-weight: normal"><SPAN lang=EN-US
style="FONT-SIZE: 14pt">struct</SPAN></STRONG></FONT><STRONG
style="mso-bidi-font-weight: normal"><SPAN
style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的成员对齐</SPAN></STRONG><STRONG
style="mso-bidi-font-weight: normal"><SPAN lang=EN-US
style="FONT-SIZE: 14pt"> </SPAN></STRONG></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18pt; mso-char-indent-count: 1.5"><SPAN
lang=EN-US style="FONT-SIZE: 12pt"><FONT
face="Times New Roman">Intel</FONT></SPAN><SPAN
style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、微软等公司曾经出过一道类似的面试题:</SPAN><SPAN
lang=EN-US style="FONT-SIZE: 12pt"> </SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 41.05pt; mso-char-indent-count: 3.42"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">1. #include <iostream.h> </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 41.05pt; mso-char-indent-count: 3.42"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 41.05pt; mso-char-indent-count: 3.42"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">2. #pragma pack(8) </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 41.05pt; mso-char-indent-count: 3.42"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">3. struct example1 </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 41.05pt; mso-char-indent-count: 3.42"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">4. { </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 41.05pt; mso-char-indent-count: 3.42"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">5. short a; </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 41.05pt; mso-char-indent-count: 3.42"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman"><SPAN
style="mso-tab-count: 1"> </SPAN>6.
long b; </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 41.05pt; mso-char-indent-count: 3.42"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman">7. }; </FONT></SPAN></P>
<P> </P>
<P class=MsoNormal
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -