﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-研发里程表</title><link>http://www.cppblog.com/wangjinhu/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 07 Apr 2026 13:48:04 GMT</lastBuildDate><pubDate>Tue, 07 Apr 2026 13:48:04 GMT</pubDate><ttl>60</ttl><item><title>有个题目考的是下面的一段C/C++代码</title><link>http://www.cppblog.com/wangjinhu/archive/2009/08/14/93276.html</link><dc:creator>wangjinhu</dc:creator><author>wangjinhu</author><pubDate>Fri, 14 Aug 2009 01:53:00 GMT</pubDate><guid>http://www.cppblog.com/wangjinhu/archive/2009/08/14/93276.html</guid><wfw:comment>http://www.cppblog.com/wangjinhu/comments/93276.html</wfw:comment><comments>http://www.cppblog.com/wangjinhu/archive/2009/08/14/93276.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/wangjinhu/comments/commentRss/93276.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wangjinhu/services/trackbacks/93276.html</trackback:ping><description><![CDATA[<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;str[</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">],</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">str;<br>scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,p[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]);<br><br>&nbsp;<br>&nbsp;<br><br>这段代码正确吗？<br><br>&#8230;<br><br>将错就错换一个考法吧，如何让这段代码正常运行呢？<br><br>下面列了我能想到的几个方法<br><br></span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;通过宏<br><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;scanf(<img src="http://www.cppblog.com/Images/dot.gif">)&nbsp;&nbsp;scanf("%s",&amp;str[2])</span><span style="COLOR: #000000"><br><br>&nbsp;<br>&nbsp;<br><br></span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;重载<br><br>&nbsp;<br><br></span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"><br>{<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;scanf(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">pfmt,&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">pR)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">pR;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;::scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,p);<br>}<br>};<br>&nbsp;<br><br>&nbsp;<br><br></span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;还是宏<br><br>&nbsp;<br><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;char&nbsp;unsigned&nbsp;long</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;str[</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">],</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">str;<br><br>p[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(unsigned&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">p[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">];<br>scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,p[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]);<br>&nbsp;<br><br>&nbsp;<br><br><br>&nbsp;&nbsp;<br><br></span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;或者插入<br><br>unsigned&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;pdw[</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">];<br>&nbsp;pdw[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(unsigned&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">p[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">];<br>&nbsp;</span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;p&nbsp;pdw</span><span style="COLOR: #000000"><br>&nbsp;<br><br><br>&nbsp;<br><br></span></div>
<img src ="http://www.cppblog.com/wangjinhu/aggbug/93276.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wangjinhu/" target="_blank">wangjinhu</a> 2009-08-14 09:53 <a href="http://www.cppblog.com/wangjinhu/archive/2009/08/14/93276.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>