📄 深入理解c指针之三(指针和结构类型的关系)_月虫_lupa开源社区.htm
字号:
face="Times New Roman">*pstr</FONT><FONT face=宋体>;</FONT><FONT
face="Times New Roman">//</FONT><FONT face=宋体>访问了</FONT><FONT
face="Times New Roman">ss</FONT><FONT face=宋体>的成员</FONT><FONT
face="Times New Roman">a</FONT><FONT face=宋体>。</FONT><FONT
face="Times New Roman"> </FONT></FONT></FONT><BR><SPAN
style="DISPLAY: none">b-v`*}GXrLE!f0</SPAN><FONT size=3><FONT
color=#000000><FONT face="Times New Roman">*(pstr+1);//</FONT><FONT
face=宋体>访问了</FONT><FONT face="Times New Roman">ss</FONT><FONT
face=宋体>的成员</FONT><FONT face="Times New Roman">b</FONT><FONT
face=宋体>。</FONT><FONT face="Times New Roman"> </FONT></FONT></FONT><BR><SPAN
style="DISPLAY: none">!eR4X#l+W%{V)|3v0</SPAN><FONT size=3><FONT
color=#000000><FONT face="Times New Roman">*(pstr+2)//</FONT><FONT
face=宋体>访问了</FONT><FONT face="Times New Roman">ss</FONT><FONT
face=宋体>的成员</FONT><FONT face="Times New Roman">c</FONT><FONT
face=宋体>。</FONT><FONT face="Times New Roman"> </FONT></FONT></FONT><BR><SPAN
style="DISPLAY: none">zS[FA-A0</SPAN><FONT size=3><FONT color=#000000><FONT
face=宋体> 虽然我在我的</FONT><FONT face="Times New Roman">MSVC++6.0</FONT><FONT
face=宋体>上调式过上述代码,但是要知道,这样使用</FONT><FONT face="Times New Roman">pstr</FONT><FONT
face=宋体>来访问结构成员是不正规的,为了说明为什么不正规,让我们看看怎样通过指针来访问数组的各个单元:</FONT><FONT
face="Times New Roman"> </FONT></FONT></FONT><SPAN
style="DISPLAY: none">LUPA开源社区{ VYRJRWZ</SPAN><BR><FONT size=3><FONT
color=#000000><FONT face=宋体> 例</FONT><FONT face="Times New Roman">6</FONT><FONT
face=宋体>:</FONT><FONT face="Times New Roman"> </FONT></FONT></FONT><BR><SPAN
style="DISPLAY: none">%aI F ]x0</SPAN><FONT face="Times New Roman"><FONT
size=3><FONT color=#000000>int array[3]={35,56,37}; </FONT></FONT></FONT><SPAN
style="DISPLAY: none">LUPA开源社区 C&aY^,DM K0gG</SPAN><BR><FONT
face="Times New Roman"><FONT size=3><FONT color=#000000>int*pa=array;
</FONT></FONT></FONT><BR><SPAN
style="DISPLAY: none">I3Y0in4h,cn:\0</SPAN><FONT size=3><FONT
color=#000000><FONT face=宋体> 通过指针</FONT><FONT
face="Times New Roman">pa</FONT><FONT face=宋体>访问数组</FONT><FONT
face="Times New Roman">array</FONT><FONT face=宋体>的三个单元的方法是:</FONT><FONT
face="Times New Roman"> </FONT></FONT></FONT><BR><SPAN style="DISPLAY: none">T
\7\de(G0</SPAN><FONT size=3><FONT color=#000000><FONT
face="Times New Roman">*pa;//</FONT><FONT face=宋体>访问了第</FONT><FONT
face="Times New Roman">0</FONT><FONT face=宋体>号单元</FONT><FONT
face="Times New Roman"> </FONT></FONT></FONT><SPAN
style="DISPLAY: none">LUPA开源社区z8gU0V)a4ky%T</SPAN><BR><FONT size=3><FONT
color=#000000><FONT face="Times New Roman">*(pa+1);//</FONT><FONT
face=宋体>访问了第</FONT><FONT face="Times New Roman">1</FONT><FONT
face=宋体>号单元</FONT><FONT face="Times New Roman"> <FONT
color=red></FONT></FONT></FONT></FONT><BR><SPAN
style="DISPLAY: none">$i}_y2~i XJq\J0</SPAN><FONT size=3><FONT
color=#000000><FONT face="Times New Roman">*(pa+2);//</FONT><FONT
face=宋体>访问了第</FONT><FONT face="Times New Roman">2</FONT><FONT
face=宋体>号单元</FONT><FONT face="Times New Roman"> </FONT></FONT></FONT><SPAN
style="DISPLAY: none">LUPA开源社区`p-eT? L6j+h*Y</SPAN><BR><FONT
color=#000000><FONT size=3><FONT
face=宋体>从格式上看倒是与通过指针访问结构成员的不正规方法的格式一样。</FONT><FONT face="Times New Roman">
</FONT></FONT></FONT><SPAN style="DISPLAY: none">LUPA开源社区/z/o
X{[K</SPAN><BR><FONT size=3><FONT face=宋体><FONT color=#000000> </FONT><FONT
color=red> </FONT><B><FONT color=#000000>所有的</FONT></B></FONT><FONT
color=#000000><B><FONT face="Times New Roman">C/C++</FONT></B><B><FONT
face=宋体>编译器在排列数组的单元时,总是把各个数组单元存放在连续的存储区里,单元和单元之间没有空隙。但在存放结构对象的各个成员时,在某种编译环境下,可能会需要字对齐或双字对齐或者是别的什么对齐,需要在相邻两个成员之间加若干个</FONT><FONT
face="Times New Roman">"</FONT></B><B><FONT face=宋体>填充字节</FONT><FONT
face="Times New Roman">"</FONT></B><B><FONT
face=宋体>,这就导致各个成员之间可能会有若干个字节的空隙。</FONT><FONT face="Times New Roman">
</FONT></B></FONT></FONT><SPAN style="DISPLAY: none">LUPA开源社区t9Mg%^
Fon-e1bB</SPAN><BR><B><FONT color=#000000><FONT size=3><FONT
face=宋体> 所以,在例</FONT>5<FONT face=宋体>中,即使</FONT>*pstr<FONT
face=宋体>访问到了结构对象</FONT>ss<FONT face=宋体>的第一个成员变量</FONT>a<FONT
face=宋体>,也不能证</FONT>*(pstr+1)<FONT face=宋体>就一定能访问到结构成员</FONT>b<FONT
face=宋体>。因为成员</FONT>a<FONT face=宋体>和成员</FONT>b<FONT
face=宋体>之间可能会有若干填充字节,说不定</FONT>*(pstr+1)<FONT
face=宋体>就正好访问到了这些填充字节呢。这也证明了指针的灵活性。要是你的目的就是想看看各个结构成员之间到底有没有填充字节,嘿,这倒是个不错的方法。</FONT></FONT></FONT></B></DIV><BR>
<DIV class=xspace-imginlog></DIV></DIV>
<P class=xspace-itemlinks><A
onclick="showajaxdiv('emailfriend', 'http://www.lupaworld.com/batch.common.php?action=emailfriend&itemid=16943', 400);"
href="javascript:;">推荐</A> <A onclick=joinfavorite(16943);
href="javascript:;">收藏</A> <A
href="http://www.lupaworld.com/bbs/post.php?action=import&itemid=16943"
target=_blank>导入论坛</A> <A
onclick="showajaxdiv('grade', 'http://www.lupaworld.com/batch.common.php?action=grade&itemid=16943', 300);"
href="javascript:;">等级(0)</A> <A
href="http://www.lupaworld.com/spacecp.php?action=spaceblogs&op=edit&itemid=16943&openwindow=1">编辑</A>
<A href="http://www.lupaworld.com/batch.manage.php?itemid=16943"
target=_blank>管理</A> <EM>查看(13)</EM> <EM>评论(0)</EM> 评分(<SPAN
class=xspace-rategood title=好评>0</SPAN>/<SPAN class=xspace-ratebad
title=差评>0</SPAN>) </P>
<P class=xspace-itemtag>TAG: </P>
<DIV id=xspace-itemreply>
<DIV class=xspace-multipage id=xspace-multipage-div><A
onclick=javascript:showcomment(1); href="javascript:;">查看全部评论</A></DIV></DIV>
<DIV id=xspace-itemform>
<FORM id=xspace-commentform action=http://www.lupaworld.com/batch.comment.php
method=post target=_self>
<FIELDSET><LEGEND>我来说两句</LEGEND>
<P class=xspace-rates0 id=xspace-rates><LABEL>评分</LABEL> <A id=xspace-rate-5
onclick="setRate('-5');" href="javascript:;">-5</A> <A id=xspace-rate-3
onclick="setRate('-3');" href="javascript:;">-3</A> <A id=xspace-rate-1
onclick="setRate('-1');" href="javascript:;">-1</A> <A id=xspace-rate0
onclick="setRate('0');" href="javascript:;">-</A> <A id=xspace-rate1
onclick="setRate('1');" href="javascript:;">+1</A> <A id=xspace-rate3
onclick="setRate('3');" href="javascript:;">+3</A> <A id=xspace-rate5
onclick="setRate('5');" href="javascript:;">+5</A> <INPUT id=xspace-ratevalue
type=hidden value=0 name=rates> </P>
<P><LABEL for=xspace-commentmsg>内容</LABEL> <TEXTAREA id=xspace-commentmsg name=message></TEXTAREA> </P>
<P><LABEL for=xspace-nickname>昵称</LABEL> <INPUT id=xspace-nickname
name=nickname> </P>
<P class=xspace-seccodeline><LABEL for=xspace-seccode>验证</LABEL> <INPUT
id=xspace-seccode size=10 name=seccode> <LABEL for=xspace-seccode><IMG
id=xspace-imgseccode title=看不清?点击换一个 onclick=javascript:newseccode(this);
alt=seccode src=""></LABEL> </P>
<P><INPUT type=hidden value=16943 name=itemid><BUTTON id=xspace-btncomment
name=submitcomment type=submit value="true">提交评论</BUTTON> </P></FIELDSET>
</FORM></DIV>
<SCRIPT language=javascript type=text/javascript>
<!--
function showcomment(page) {
var x = new Ajax('statusid', 'XML');
x.get('http://www.lupaworld.com/batch.comment.php?action=view&itemid=16943&page='+page, function(s) {
getbyid("xspace-itemreply").innerHTML = s.lastChild.firstChild.nodeValue;
});
}
function deletecomment(cid) {
var x = new Ajax('statusid', 'XML');
x.get('http://www.lupaworld.com/batch.comment.php?action=delete&cid='+cid, function(s) {
alert(s.lastChild.firstChild.nodeValue);
});
}
function newseccode(obj) {
obj.src='http://www.lupaworld.com/batch.seccode.php?'+Math.random(1);
}
addImgLink("xspace-showmessage");
addMediaAction('xspace-showmessage');
//-->
</SCRIPT>
</DIV></DIV></DIV></DIV>
<DIV id=xspace-footer>
<P id=xspace-footer-ad>
<DIV style="DISPLAY: none">
<SCRIPT language=JavaScript
src="深入理解C指针之三(指针和结构类型的关系)_月虫_LUPA开源社区.files/stat.htm" charset=gb2312></SCRIPT>
</DIV>
<P></P>
<P id=xspace-footer_menu><A
href="http://www.lupaworld.com/batch.login.php?action=logout">清空Cookie</A> - <A
href="mailto:webmaster@lupaworld.com">联系我们</A> - <A
href="http://www.lupaworld.com/" target=_blank>LUPA开源社区</A> - <A
href="http://www.lupaworld.com/action_register.html" target=_blank>申请自己的空间</A>
</P>
<P id=xspace-copyright><BR><A href="http://www.miibeian.gov.cn/"
target=_blank>浙ICP备06002895号</A><BR></P></DIV>
<DIV id=toolbarshowbtn><A onmouseover=showtoolbar(); href="javascript:;">Open
Toolbar</A></DIV>
<DIV id=toolbarframe><IFRAME
src="深入理解C指针之三(指针和结构类型的关系)_月虫_LUPA开源社区.files/batch.toolbar.php" frameBorder=0
width="100%" scrolling=no></IFRAME></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -