📄 [求助]请教指针类型强制转换问题? - c语言 - oldlinux - powered by discuz!.htm
字号:
vAlign=top width="82%">
<TABLE class=t_msg cellSpacing=0 cellPadding=4 border=0>
<TBODY>
<TR>
<TD>
<DIV>
<DIV class="right t_number"><A class=bold title=复制帖子链接到剪贴板
onclick="setcopy('http://www.oldlinux.org/oldlinux/viewthread.php?tid=8282&page=1#pid33551', '已经复制到剪贴板')"
href="http://www.oldlinux.org/oldlinux/viewthread.php?tid=8282###">#2</A></DIV>
<DIV class=right style="PADDING-TOP: 4px"><A
href="http://www.oldlinux.org/oldlinux/magic.php?action=user&pid=33551"
target=_blank>使用道具</A> </DIV>
<DIV style="PADDING-TOP: 4px">发表于 2007-4-8 23:15 <A
href="http://www.oldlinux.org/oldlinux/viewpro.php?uid=12195"
target=_blank>资料</A> <A title=wildcatgsk的个人空间
href="http://www.oldlinux.org/oldlinux/space.php?uid=12195"
target=_blank>个人空间</A> <A
href="http://www.oldlinux.org/oldlinux/pm.php?action=send&uid=12195"
target=_blank>短消息</A> <A id=ajax_buddy_1
onclick="ajaxmenu(event, this.id)"
href="http://www.oldlinux.org/oldlinux/memcp.php?action=buddylist&newbuddyid=12195&buddysubmit=yes"
target=_blank>加为好友</A> </DIV></DIV></TD></TR>
<TR>
<TD class=line style="PADDING-TOP: 10px" vAlign=top height="100%">
<DIV style="FLOAT: right" align=right><A title="评分 0"
href="http://www.oldlinux.org/oldlinux/misc.php?action=viewratings&tid=8282&pid=33551"></A></DIV><SPAN
class=bold>[求助]请教指针类型强制转换问题?</SPAN><BR><BR>
<DIV class=t_msgfont id=message33551>[这个贴子最后由wildcatgsk在 2007/04/08
11:18pm 第 2 次编辑] 这个问题不是这样考虑的,其实要搞清楚的是指针加法比如 int * i; char * c; ....
你可以在赋值以后试下i+1、c+1后各自的指针与i、c的差别。所以(char
*)只是通知编译器改变以后对某个地址(指针的本质就是地址)的处理方法。举个例子来说: #include <STDIO.H>int
main(void){ int *ip; int a[2]={0x301,0x44444444}; ip = a;
printf("*ip=%x, *(ip+1)=%x\n",*ip,*(ip+1)); printf("*(char *)ip=%x,
*((char *)ip+1))=%x",*(char *)ip,*((char *)ip+1)); return 0; }
你不妨分析一下这段代码的结果就明白了,编译器改变了对某个地址的处理方法,如此而已,并不是该地址真的“变成了”字符串</DIV><BR
style="CLEAR: both"><FONT
style="FONT: 12px Tahoma, Verdana; COLOR: #333333"></FONT></TD></TR>
<TR>
<TD align=right><A onclick=scroll(0,0)
href="http://www.oldlinux.org/oldlinux/viewthread.php?tid=8282###"><IMG
alt=顶部
src="[求助]请教指针类型强制转换问题? - C语言 - OldLinux - Powered by Discuz!.files/top.gif"
border=0></A> </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV>
<DIV class=spaceborder style="MARGIN-BOTTOM: 4px; WIDTH: 98%">
<TABLE class=t_row cellSpacing=0 cellPadding=4 width="100%" align=center>
<TBODY>
<TR style="HEIGHT: 100%">
<TD class=t_user vAlign=top width="18%"><A name=pid33552></A><A class=bold
href="http://www.oldlinux.org/oldlinux/space.php?uid=8795"
target=_blank>porsche</A> <BR>
<DIV class=smalltxt>新手上路<BR><IMG alt="Rank: 1"
src="[求助]请教指针类型强制转换问题? - C语言 - OldLinux - Powered by Discuz!.files/star_level1.gif"><BR>Member<BR><BR><BR><BR>UID
8795<BR>精华 0<BR>积分 21<BR>帖子 15<BR>阅读权限 10<BR>注册 2007-3-21<BR>状态 离线
</DIV></TD>
<TD
style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px"
vAlign=top width="82%">
<TABLE class=t_msg cellSpacing=0 cellPadding=4 border=0>
<TBODY>
<TR>
<TD>
<DIV>
<DIV class="right t_number"><A class=bold title=复制帖子链接到剪贴板
onclick="setcopy('http://www.oldlinux.org/oldlinux/viewthread.php?tid=8282&page=1#pid33552', '已经复制到剪贴板')"
href="http://www.oldlinux.org/oldlinux/viewthread.php?tid=8282###">#3</A></DIV>
<DIV class=right style="PADDING-TOP: 4px"><A
href="http://www.oldlinux.org/oldlinux/magic.php?action=user&pid=33552"
target=_blank>使用道具</A> </DIV>
<DIV style="PADDING-TOP: 4px">发表于 2007-4-9 08:42 <A
href="http://www.oldlinux.org/oldlinux/viewpro.php?uid=8795"
target=_blank>资料</A> <A title=porsche的个人空间
href="http://www.oldlinux.org/oldlinux/space.php?uid=8795"
target=_blank>个人空间</A> <A
href="http://www.oldlinux.org/oldlinux/pm.php?action=send&uid=8795"
target=_blank>短消息</A> <A id=ajax_buddy_2
onclick="ajaxmenu(event, this.id)"
href="http://www.oldlinux.org/oldlinux/memcp.php?action=buddylist&newbuddyid=8795&buddysubmit=yes"
target=_blank>加为好友</A> </DIV></DIV></TD></TR>
<TR>
<TD class=line style="PADDING-TOP: 10px" vAlign=top height="100%">
<DIV style="FLOAT: right" align=right><A title="评分 0"
href="http://www.oldlinux.org/oldlinux/misc.php?action=viewratings&tid=8282&pid=33552"></A></DIV><SPAN
class=bold>[求助]请教指针类型强制转换问题?</SPAN><BR><BR>
<DIV class=t_msgfont
id=message33552>谢谢指点,有点搞明白了。其实放在内存的内容都是二进制编码,只是拿出来的类型依照指针类型而定,是吗?如果是,那上面的&#35;define
get_base(ldt) _get_base( ((char *)&(ldt))
)的char可以改成任意型的指针,因为涉及的汇编代码直与取内存值到寄存器有关,可以这样改吗?<BR>[br][br]<FONT
color=#990000><B>-=-=-=- 以下内容由 <I>porsche</I> 在 <I>2007年04月09日
08:56am</I> 时添加
-=-=-=-</B></FONT><BR>为什么不直接使用地址操作了,还要转换成指针?<BR>&#35;define
get_base(ldt) _get_base( ((char *)&(ldt)) )<BR>&#35;define
_get_base(addr) ({\<BR>unsigned long __base; \<BR>__asm__("movb
%3,%%dh\n\t" \<BR>"movb %2,%%dl\n\t" \<BR>"shll
&#36;16,%%edx\n\t" \<BR>"movw %1,%%dx" \<BR>:"=d" (__base)
\<BR>:"m" (*((addr)+2)), \<BR>"m" (*((addr)+4)), \<BR>"m"
(*((addr)+7))); \<BR>__base;})</DIV><BR style="CLEAR: both"><FONT
style="FONT: 12px Tahoma, Verdana; COLOR: #333333"></FONT></TD></TR>
<TR>
<TD align=right><A onclick=scroll(0,0)
href="http://www.oldlinux.org/oldlinux/viewthread.php?tid=8282###"><IMG
alt=顶部
src="[求助]请教指针类型强制转换问题? - C语言 - OldLinux - Powered by Discuz!.files/top.gif"
border=0></A> </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV>
<DIV class=spaceborder style="MARGIN-BOTTOM: 4px; WIDTH: 98%">
<TABLE class=t_row cellSpacing=0 cellPadding=4 width="100%" align=center>
<TBODY>
<TR style="HEIGHT: 100%">
<TD class=t_user vAlign=top width="18%"><A name=pid33553></A><A class=bold
href="http://www.oldlinux.org/oldlinux/space.php?uid=12195"
target=_blank>wildcatgsk</A> <BR>
<DIV class=smalltxt>版主<BR><IMG alt="Rank: 7"
src="[求助]请教指针类型强制转换问题? - C语言 - OldLinux - Powered by Discuz!.files/star_level3.gif"><IMG
alt="Rank: 7"
src="[求助]请教指针类型强制转换问题? - C语言 - OldLinux - Powered by Discuz!.files/star_level2.gif"><IMG
alt="Rank: 7"
src="[求助]请教指针类型强制转换问题? - C语言 - OldLinux - Powered by Discuz!.files/star_level1.gif"><BR>Member<BR><BR><BR><BR>UID
12195<BR>精华 <A
href="http://www.oldlinux.org/oldlinux/digest.php?authorid=12195">6</A>
<BR>积分 415<BR>帖子 322<BR>阅读权限 100<BR>注册 2004-5-13<BR>状态 离线 </DIV></TD>
<TD
style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px"
vAlign=top width="82%">
<TABLE class=t_msg cellSpacing=0 cellPadding=4 border=0>
<TBODY>
<TR>
<TD>
<DIV>
<DIV class="right t_number"><A class=bold title=复制帖子链接到剪贴板
onclick="setcopy('http://www.oldlinux.org/oldlinux/viewthread.php?tid=8282&page=1#pid33553', '已经复制到剪贴板')"
href="http://www.oldlinux.org/oldlinux/viewthread.php?tid=8282###">#4</A></DIV>
<DIV class=right style="PADDING-TOP: 4px"><A
href="http://www.oldlinux.org/oldlinux/magic.php?action=user&pid=33553"
target=_blank>使用道具</A> </DIV>
<DIV style="PADDING-TOP: 4px">发表于 2007-4-9 08:59 <A
href="http://www.oldlinux.org/oldlinux/viewpro.php?uid=12195"
target=_blank>资料</A> <A title=wildcatgsk的个人空间
href="http://www.oldlinux.org/oldlinux/space.php?uid=12195"
target=_blank>个人空间</A> <A
href="http://www.oldlinux.org/oldlinux/pm.php?action=send&uid=12195"
target=_blank>短消息</A> <A id=ajax_buddy_3
onclick="ajaxmenu(event, this.id)"
href="http://www.oldlinux.org/oldlinux/memcp.php?action=buddylist&newbuddyid=12195&buddysubmit=yes"
target=_blank>加为好友</A> </DIV></DIV></TD></TR>
<TR>
<TD class=line style="PADDING-TOP: 10px" vAlign=top height="100%">
<DIV style="FLOAT: right" align=right><A title="评分 0"
href="http://www.oldlinux.org/oldlinux/misc.php?action=viewratings&tid=8282&pid=33553"></A></DIV><SPAN
class=bold>[求助]请教指针类型强制转换问题?</SPAN><BR><BR>
<DIV class=t_msgfont
id=message33553>你理解得是对的,只是还要再理解透彻一点<BR>你说“其实放在内存的内容都是二进制编码”,完全正确,而且是问题的关键,但后面这句“只是拿出来的类型依照指针类型而定”虽然也是对的,但不是很完整,所以我不知道你是不是真的理解了,后面这句应该这样说,“只是在处理内存时是依照指针类型而定”。<BR>比如用(char
*)作强制转换,则将来处理相应的内存地址时是按字节为单位处理的,如果用(int
*)则将按4个字节为单位进行处理,它们的差异会是巨大的,如果你理解了这一点就不会认为&#35;define
get_base(ldt) _get_base( ((char *)&(ldt))
)的char可以改成任意型的指针。其实应该用什么类型的指针取决于_get_base具体是实现什么功能,如果在它内部需要按字节去操作内存单元,那肯定不能替换这里的char,如果只是如其字面“获得基地址”,那到是用什么指针类型都行.</DIV><BR
style="CLEAR: both"><FONT
style="FONT: 12px Tahoma, Verdana; COLOR: #333333"></FONT></TD></TR>
<TR>
<TD align=right><A onclick=scroll(0,0)
href="http://www.oldlinux.org/oldlinux/viewthread.php?tid=8282###"><IMG
alt=顶部
src="[求助]请教指针类型强制转换问题? - C语言 - OldLinux - Powered by Discuz!.files/top.gif"
border=0></A> </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV>
<DIV class=spaceborder style="MARGIN-BOTTOM: 4px; WIDTH: 98%">
<TABLE class=t_row cellSpacing=0 cellPadding=4 width="100%" align=center>
<TBODY>
<TR style="HEIGHT: 100%">
<TD class=t_user vAlign=top width="18%"><A name=pid33554></A><A
name=lastpost></A><A class=bold
href="http://www.oldlinux.org/oldlinux/space.php?uid=8795"
target=_blank>porsche</A> <BR>
<DIV class=smalltxt>新手上路<BR><IMG alt="Rank: 1"
src="[求助]请教指针类型强制转换问题? - C语言 - OldLinux - Powered by Discuz!.files/star_level1.gif"><BR>Member<BR><BR><BR><BR>UID
8795<BR>精华 0<BR>积分 21<BR>帖子 15<BR>阅读权限 10<BR>注册 2007-3-21<BR>状态 离线
</DIV></TD>
<TD
style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px"
vAlign=top width="82%">
<TABLE class=t_msg cellSpacing=0 cellPadding=4 border=0>
<TBODY>
<TR>
<TD>
<DIV>
<DIV class="right t_number"><A class=bold title=复制帖子链接到剪贴板
onclick="setcopy('http://www.oldlinux.org/oldlinux/viewthread.php?tid=8282&page=1#pid33554', '已经复制到剪贴板')"
href="http://www.oldlinux.org/oldlinux/viewthread.php?tid=8282###">#5</A></DIV>
<DIV class=right style="PADDING-TOP: 4px"><A
href="http://www.oldlinux.org/oldlinux/magic.php?action=user&pid=33554"
target=_blank>使用道具</A> </DIV>
<DIV style="PADDING-TOP: 4px">发表于 2007-4-9 09:06 <A
href="http://www.oldlinux.org/oldlinux/viewpro.php?uid=8795"
target=_blank>资料</A> <A title=porsche的个人空间
href="http://www.oldlinux.org/oldlinux/space.php?uid=8795"
target=_blank>个人空间</A> <A
href="http://www.oldlinux.org/oldlinux/pm.php?action=send&uid=8795"
target=_blank>短消息</A> <A id=ajax_buddy_4
onclick="ajaxmenu(event, this.id)"
href="http://www.oldlinux.org/oldlinux/memcp.php?action=buddylist&newbuddyid=8795&buddysubmit=yes"
target=_blank>加为好友</A> </DIV></DIV></TD></TR>
<TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -