﻿<?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++博客-mysileng-随笔分类-C</title><link>http://www.cppblog.com/mysileng/category/16815.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 01 Dec 2012 05:38:03 GMT</lastBuildDate><pubDate>Sat, 01 Dec 2012 05:38:03 GMT</pubDate><ttl>60</ttl><item><title>memcpy与memmove(转)</title><link>http://www.cppblog.com/mysileng/archive/2012/11/25/195649.html</link><dc:creator>鑫龙</dc:creator><author>鑫龙</author><pubDate>Sun, 25 Nov 2012 08:55:00 GMT</pubDate><guid>http://www.cppblog.com/mysileng/archive/2012/11/25/195649.html</guid><wfw:comment>http://www.cppblog.com/mysileng/comments/195649.html</wfw:comment><comments>http://www.cppblog.com/mysileng/archive/2012/11/25/195649.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mysileng/comments/commentRss/195649.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mysileng/services/trackbacks/195649.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: memcpy与memmove的目的都是将N个字节的源内存地址的内容拷贝到目标内存地址中。但当源内存和目标内存存在重叠时，memcpy会出现错误，而memmove能正确地实施拷贝，但这也增加了一点点开销。memmove的处理措施：（1）当源内存的首地址等于目标内存的首地址时，不进行任何拷贝（2）当源内存的首地址大于目标内存的首地址时，实行正向拷贝（3）当源内存的首地址小于目标内存的首地址时，实行反向...&nbsp;&nbsp;<a href='http://www.cppblog.com/mysileng/archive/2012/11/25/195649.html'>阅读全文</a><img src ="http://www.cppblog.com/mysileng/aggbug/195649.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mysileng/" target="_blank">鑫龙</a> 2012-11-25 16:55 <a href="http://www.cppblog.com/mysileng/archive/2012/11/25/195649.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(i++)+(i++)与(++i)+(++i) (转)</title><link>http://www.cppblog.com/mysileng/archive/2012/11/19/195380.html</link><dc:creator>鑫龙</dc:creator><author>鑫龙</author><pubDate>Mon, 19 Nov 2012 12:16:00 GMT</pubDate><guid>http://www.cppblog.com/mysileng/archive/2012/11/19/195380.html</guid><wfw:comment>http://www.cppblog.com/mysileng/comments/195380.html</wfw:comment><comments>http://www.cppblog.com/mysileng/archive/2012/11/19/195380.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mysileng/comments/commentRss/195380.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mysileng/services/trackbacks/195380.html</trackback:ping><description><![CDATA[<div id="cnblogs_post_body">
<p>与在前面:++(--)有太多让人困惑的地方,(i++)+(i++)与(++i)+(++i)有什么不同?为什么不同?如果从机器的角度去理解，就会豁然开朗。</p>
<p>&nbsp;先来看段程序:</p>
<div class="cnblogs_code">
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #800080">3</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">(i</span><span style="color: #000000">++</span><span style="color: #000000">)</span><span style="color: #000000">+</span><span style="color: #000000">(i</span><span style="color: #000000">++</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;j=(++i)+(++i);</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #800000">"</span><span style="color: #800000">%d,%d\n</span><span style="color: #800000">"</span><span style="color: #000000">,i,j);<br />}</span> 
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div></div>
<p><span style="color: #0000ff">(1)在VC 6.0下：</span><br /><br />对于(i++)+(i++):<br />结果：i=5,j=6<br /><br />相应的汇编代码为(有详细注释)：</p>
<div class="cnblogs_code">
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #000000">8B&nbsp;</span><span style="color: #800080">45</span><span style="color: #000000">&nbsp;FC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eax,dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">4</span><span style="color: #000000">]&nbsp;&nbsp; ;i</span><span style="color: #000000">-&gt;</span><span style="color: #000000">eax<br /></span><span style="color: #800080">03</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">45</span><span style="color: #000000">&nbsp;FC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eax,dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">4</span><span style="color: #000000">]&nbsp;&nbsp;&nbsp;&nbsp;;i</span><span style="color: #000000">+</span><span style="color: #000000">i</span><span style="color: #000000">=</span><span style="color: #800080">6</span><span style="color: #000000"><br /></span><span style="color: #800080">89</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">45</span><span style="color: #000000">&nbsp;F8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">8</span><span style="color: #000000">],eax&nbsp;&nbsp;&nbsp;&nbsp;;</span><span style="color: #800080">6</span><span style="color: #000000">-&gt;</span><span style="color: #000000">j<br />8B&nbsp;4D&nbsp;FC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ecx,dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">4</span><span style="color: #000000">]&nbsp;&nbsp;&nbsp;&nbsp;;i</span><span style="color: #000000">-&gt;</span><span style="color: #000000">ecx(</span><span style="color: #000000">=</span><span style="color: #800080">3</span><span style="color: #000000">)<br /></span><span style="color: #800080">83</span><span style="color: #000000">&nbsp;C1&nbsp;</span><span style="color: #800080">01</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ecx,</span><span style="color: #800080">1</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;ecx</span><span style="color: #000000">=</span><span style="color: #800080">4</span><span style="color: #000000"><br /></span><span style="color: #800080">89</span><span style="color: #000000">&nbsp;4D&nbsp;FC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">4</span><span style="color: #000000">],ecx&nbsp;&nbsp;&nbsp;&nbsp;;</span><span style="color: #800080">4</span><span style="color: #000000">-&gt;</span><span style="color: #000000">i<br />8B&nbsp;</span><span style="color: #800080">55</span><span style="color: #000000">&nbsp;FC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edx,dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">4</span><span style="color: #000000">]&nbsp;&nbsp;&nbsp;&nbsp;;i</span><span style="color: #000000">-&gt;</span><span style="color: #000000">edx<br /></span><span style="color: #800080">83</span><span style="color: #000000">&nbsp;C2&nbsp;</span><span style="color: #800080">01</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edx,</span><span style="color: #800080">1</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;edx</span><span style="color: #000000">=</span><span style="color: #800080">5</span><span style="color: #000000"><br /></span><span style="color: #800080">89</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">55</span><span style="color: #000000">&nbsp;FC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">4</span><span style="color: #000000">],edx&nbsp;&nbsp;&nbsp;&nbsp;;</span><span style="color: #800080">5</span><span style="color: #000000">-&gt;</span><span style="color: #000000">i</span> 
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div></div>
<p>&nbsp;<br />对于(++i)+(++i)：<br />结果：i=5,j=10<br />相应的汇编代码为：</p>
<div class="cnblogs_code">
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #000000">8B&nbsp;</span><span style="color: #800080">45</span><span style="color: #000000">&nbsp;FC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eax,dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">4</span><span style="color: #000000">]&nbsp;&nbsp;&nbsp;&nbsp;;i</span><span style="color: #000000">-&gt;</span><span style="color: #000000">eax&nbsp;(</span><span style="color: #000000">=</span><span style="color: #800080">3</span><span style="color: #000000">)<br /></span><span style="color: #800080">83</span><span style="color: #000000">&nbsp;C0&nbsp;</span><span style="color: #800080">01</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eax,</span><span style="color: #800080">1</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;eax</span><span style="color: #000000">=</span><span style="color: #800080">4</span><span style="color: #000000"><br /></span><span style="color: #800080">89</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">45</span><span style="color: #000000">&nbsp;FC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">4</span><span style="color: #000000">],eax&nbsp;&nbsp;&nbsp;&nbsp;;</span><span style="color: #800080">4</span><span style="color: #000000">-&gt;</span><span style="color: #000000">i<br />8B&nbsp;4D&nbsp;FC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ecx,dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">4</span><span style="color: #000000">]&nbsp;&nbsp;&nbsp;&nbsp;;i</span><span style="color: #000000">-&gt;</span><span style="color: #000000">ecx<br /></span><span style="color: #800080">83</span><span style="color: #000000">&nbsp;C1&nbsp;</span><span style="color: #800080">01</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ecx,</span><span style="color: #800080">1</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;ecx</span><span style="color: #000000">=</span><span style="color: #800080">5</span><span style="color: #000000"><br /></span><span style="color: #800080">89</span><span style="color: #000000">&nbsp;4D&nbsp;FC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">4</span><span style="color: #000000">],ecx&nbsp;&nbsp;&nbsp;&nbsp;;</span><span style="color: #800080">5</span><span style="color: #000000">-&gt;</span><span style="color: #000000">i<br />8B&nbsp;</span><span style="color: #800080">55</span><span style="color: #000000">&nbsp;FC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edx,dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">4</span><span style="color: #000000">]&nbsp;&nbsp;&nbsp;&nbsp;;i</span><span style="color: #000000">-&gt;</span><span style="color: #000000">edx<br /></span><span style="color: #800080">03</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">55</span><span style="color: #000000">&nbsp;FC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edx,dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">4</span><span style="color: #000000">]&nbsp;&nbsp;&nbsp;&nbsp;;edx</span><span style="color: #000000">=</span><span style="color: #800080">10</span><span style="color: #000000">&nbsp;,即i</span><span style="color: #000000">+</span><span style="color: #000000">i<br /></span><span style="color: #800080">89</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">55</span><span style="color: #000000">&nbsp;F8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dword&nbsp;ptr&nbsp;[ebp</span><span style="color: #000000">-</span><span style="color: #800080">8</span><span style="color: #000000">],edx&nbsp;&nbsp;&nbsp;&nbsp;;</span><span style="color: #800080">10</span><span style="color: #000000">-&gt;</span><span style="color: #000000">j</span> 
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div></div>
<p>&nbsp;<br /><span style="color: #0000ff">(2)在gcc 3.2.2下:</span></p>
<p>对于(i++)+(i++):</p>
<p>结果：i=5,j=6相应的汇编代码为：</p>
<div class="cnblogs_code">
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #000000"></span>c7 45 fc 03 00 00 00 &nbsp;&nbsp; &nbsp;movl&nbsp;&nbsp; &nbsp;$3, -4(%ebp）&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;3-&gt;i<br />8b 55 fc&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;movl&nbsp;&nbsp; &nbsp;-4(%ebp), %edx&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;i-&gt;edx (=3)<br />8b 45 fc&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;movl&nbsp;&nbsp; &nbsp;-4(%ebp), %eax&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;i-&gt;eax&nbsp;&nbsp; &nbsp;(=3)<br />8d 04 10 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;leal&nbsp;&nbsp; &nbsp;(%eax,%edx), %eax &nbsp;&nbsp; &nbsp;;i+i=6 -&gt;eax<br />89 45 f8&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;movl&nbsp;&nbsp; &nbsp;%eax, -8(%ebp)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;6-&gt;j<br />8d 45 fc&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;leal&nbsp;&nbsp; &nbsp;-4(%ebp), %eax&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;&amp;i-&gt;eax<br />ff 00&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;incl&nbsp;&nbsp; &nbsp;(%eax)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;i++ ,即i=4,注意这里为寄存器间接寻址<br />8d 45 fc&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;leal&nbsp;&nbsp; &nbsp;-4(%ebp), %eax&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;&amp;i-&gt;eax<br />ff 00&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;incl&nbsp;&nbsp; &nbsp;(%eax)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;i++,即i=5<span style="color: #800080"></span> 
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div></div>
<p>&nbsp;<br />对于(++i)+(++i)：<br />结果：i=5,j=10<br />相应的汇编代码为：</p>
<div class="cnblogs_code">
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #000000"></span>movl&nbsp;&nbsp; &nbsp;$3, -4(%ebp)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;3-&gt;i<br />leal&nbsp;&nbsp; &nbsp;-4(%ebp), %eax&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;&amp;i-&gt;eax<br />incl&nbsp;&nbsp; &nbsp;(%eax)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;i++,即i=4<br />leal&nbsp;&nbsp; &nbsp;-4(%ebp), %eax&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;&amp;i-&gt;eax<br />incl&nbsp;&nbsp; &nbsp;(%eax)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;i++, i=5<br />movl&nbsp;&nbsp; &nbsp;-4(%ebp), %eax&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;i-&gt;eax, eax=5<br />addl&nbsp;&nbsp; &nbsp;-4(%ebp), %eax&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;i+i -&gt;eax ,eax=10<br />movl&nbsp;&nbsp; &nbsp;%eax, -8(%ebp)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;10-&gt;j<span style="color: #000000"></span> 
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div></div>
<p><br />可见，对于VC6.0和gcc，二者的结果一致，但是gcc 3.2.2生成的汇编代码明显比VC6.0高效、简洁。这也许是因为VC 6.0出现较早的原因吧。</p>
<p>&nbsp;</p>
<p><span style="color: #0000ff">(3)如果这段代码用java实现，结果会怎样呢？</span></p>
<p>程序:</p>
<div class="cnblogs_code">
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;TestAdd&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">3</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">(i</span><span style="color: #000000">++</span><span style="color: #000000">)</span><span style="color: #000000">+</span><span style="color: #000000">(i</span><span style="color: #000000">++</span><span style="color: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">5,7<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">int&nbsp;j=(++i)+(++i);&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">5,9</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(i</span><span style="color: #000000">+</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">"</span><span style="color: #000000">+</span><span style="color: #000000">j);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</span> 
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div></div>
<p>&nbsp;对于(++i)+(++i):<br />i=5,j=9。结果点意外!<br />来看看它的字节码吧:</p>
<div class="cnblogs_code">
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #000000">//j=(++i)+(++i)<br />//</span><span style="color: #800080">5</span><span style="color: #000000">,</span><span style="color: #800080">9</span><span style="color: #000000"><br />&nbsp;</span><span style="color: #800080">0</span><span style="color: #000000">:&nbsp;&nbsp;&nbsp;iconst_3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">;</span><span style="color: #008000">常量3入栈</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">1</span><span style="color: #000000">:&nbsp;&nbsp;&nbsp;istore_1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">;</span><span style="color: #008000">从栈中弹出3,存入i,i=3</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">2</span><span style="color: #000000">:&nbsp;&nbsp;&nbsp;iinc&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800080">1</span><span style="color: #000000">,&nbsp;</span><span style="color: #800080">1</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000">;</span><span style="color: #008000">i++,&nbsp;i=4</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">5</span><span style="color: #000000">:&nbsp;&nbsp;&nbsp;iload_1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000">;</span><span style="color: #008000">将i压入栈,即4入栈</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">6</span><span style="color: #000000">:&nbsp;&nbsp;&nbsp;iinc&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800080">1</span><span style="color: #000000">,&nbsp;</span><span style="color: #800080">1</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000">; </span><span style="color: #008000">i++,i=5</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">9</span><span style="color: #000000">:&nbsp;&nbsp;&nbsp;iload_1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000">;</span><span style="color: #008000">i入栈,即5入栈</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">10</span><span style="color: #000000">:&nbsp;&nbsp;iadd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000">;</span><span style="color: #008000">从栈中弹出两个int类型的数相加,结果入栈,即9入栈</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">11</span><span style="color: #000000">:&nbsp;&nbsp;istore_2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">;</span><span style="color: #008000">从栈中弹出9,存入j,即j=9</span> 
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div></div>
<p>&nbsp;对于(i++)+(i++):<br />i=5,j=7。结果也很意外!<br />也来看看它的字节码吧:</p>
<div class="cnblogs_code">
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #000000">//j=(i++)+(i++)<br />//</span><span style="color: #800080">5</span><span style="color: #000000">,</span><span style="color: #800080">7</span><span style="color: #000000"><br />&nbsp;</span><span style="color: #800080">0</span><span style="color: #000000">:&nbsp;&nbsp;&nbsp;iconst_3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">;</span><span style="color: #008000">常量3入栈</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">1</span><span style="color: #000000">:&nbsp;&nbsp;&nbsp;istore_1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">;</span><span style="color: #008000">从栈中弹出3,存入i,i=3</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">2</span><span style="color: #000000">:&nbsp;&nbsp;&nbsp;iload_1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000">;</span><span style="color: #008000">i入栈,即3入栈</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">3</span><span style="color: #000000">:&nbsp;&nbsp;&nbsp;iinc&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800080">1</span><span style="color: #000000">,&nbsp;</span><span style="color: #800080">1</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000">;</span><span style="color: #008000">i++,即i=4</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">6</span><span style="color: #000000">:&nbsp;&nbsp;&nbsp;iload_1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000">;</span><span style="color: #008000">i入栈,即4入栈</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">7</span><span style="color: #000000">:&nbsp;&nbsp;&nbsp;iinc&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800080">1</span><span style="color: #000000">,&nbsp;</span><span style="color: #800080">1</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000">;</span><span style="color: #008000">i++,即i=5;注意：5没有入栈,所以此时栈中的数为3和4</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">10</span><span style="color: #000000">:&nbsp;&nbsp;iadd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000">;</span><span style="color: #008000">从栈弹出两个int类型数相加，结果入栈,即7入栈</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;</span><span style="color: #800080">11</span><span style="color: #000000">:&nbsp;&nbsp;istore_2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">;</span><span style="color: #008000">从栈中弹出7，存入j,即j=7</span> 
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif" /></a></span></div></div>
<p>&nbsp;</p>
<p>Java与VC/gcc为什么会有如此的区别呢？其实原因很简单，VC/gcc生成的是本地代码，而X86处理器是基于寄存器的架构，也就是如果它要进行了两个数相加，它会先把两个数移到寄存器，再进行加法运算。而Java虚拟机是一种基于栈的架构，如果它要进行两个数相加，它会先弹出两个数，再进行加法运算，再将结果入栈。</p></div><img src ="http://www.cppblog.com/mysileng/aggbug/195380.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mysileng/" target="_blank">鑫龙</a> 2012-11-19 20:16 <a href="http://www.cppblog.com/mysileng/archive/2012/11/19/195380.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>2维数组和指针差异</title><link>http://www.cppblog.com/mysileng/archive/2012/01/28/164611.html</link><dc:creator>鑫龙</dc:creator><author>鑫龙</author><pubDate>Sat, 28 Jan 2012 11:40:00 GMT</pubDate><guid>http://www.cppblog.com/mysileng/archive/2012/01/28/164611.html</guid><wfw:comment>http://www.cppblog.com/mysileng/comments/164611.html</wfw:comment><comments>http://www.cppblog.com/mysileng/archive/2012/01/28/164611.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mysileng/comments/commentRss/164611.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mysileng/services/trackbacks/164611.html</trackback:ping><description><![CDATA[<span style="color: #333333; font-family: Arial; font-size: 14px; line-height: 26px; "><div>数组和指针概念很容易让初学者混淆，这里把我所知道的概括一下，希望对大家有所帮助。</div><div><h2>一维数组和一级指针的相同和差异</h2><p>一维数组的名字大部分情况下可以当成一个常指针来看，也就是说：</p><pre style="word-wrap: break-word; white-space: normal; ">int a[3] int * const p;</pre><p>则数组名a可以在大部分p能用的地方使用，有两种情况比较特殊：</p><ol><li>p可以在定义是初始化一个地址如：int *p const = new int[100];，而且这也是唯一的初始化他的机会。而数组则是有操作系统载入可执行文件时初始化内存空间的。</li><li>sizeof运算符对p计算将获得指针的长度（现在一般为4），而对数组名则得到数组占有的所有字节数。</li></ol><p>在内存排布上，一维数组和一阶指针指向的内存是完全一样的。</p><h2>高维数组和高阶指针</h2><p>对于高维数组（以二维为例，其他完全一样），情况和一维数组完全不一样。例如对于数组和指针：</p><pre style="word-wrap: break-word; white-space: normal; ">int a[3][4] int**p;</pre><p>他们就没有任何可比性。初学者从一维数组中的知识简单的推断出a是一个等价于int**的东西，*a就可以得到一个 int*的值，这其实是完全错误的。</p><p>从内存排布上，数组按照先低维后高维的顺序一次排列数组的每个元素。其中低维到高维是指定义数组时，离数组名越近的维为低维，反之为高维，例如上面这个数组，3就是低维的维数，4为高维的维数，因此，在内存中，上面这个数组占有12个int单元，所有单元靠在一起，其顺序则为</p><pre style="word-wrap: break-word; white-space: normal; "><blockquote>a[0][0]，a[0][1]，a[0][2]，a[0][3] a[1][0]，a[1][1]，a[1][2]，a[1][3] a[2][0]，a[2][1]，a[2][2]，a[2][3]</blockquote></pre><p>如果a是一个和二阶指针等价的常量，那么他的值应该指向一个指针数组才对，而实际上这样的指针数组是不存在。</p><p>从前面这个排布还可以看出一个问题，在排列过程中，数组的最低维不确定是没有关系的，但是为了安排好数组元素的先后顺序，高维的维数必须确定，也就是说在使用数组类型来定义指针时，必须首先确定高维，例如：</p><pre style="word-wrap: break-word; white-space: normal; ">int (*p)[3] = new int[4][3]</pre><p>是可以的，但是</p><pre style="word-wrap: break-word; white-space: normal; ">int (*p)[] = new int[4][3]</pre><p>就不行。</p><h2>回到一维数组</h2><p>仔细考虑一维数组和多维数组，是不是编译器对一维数组特殊处理了？其实也不是，数组名其实代表的是数组对象也就是其第一个元素的地址，从这个角度讲，一维数组名a代表了a[0]的地址，二维数组名a代表了a[0][0]的地址，高维数组的名字从这个意义讲更接近1阶指针.<br /><img src="http://www.cppblog.com/images/cppblog_com/mysileng/1.jpg" border="0" alt="" width="639" height="566" /><br /></p></div></span><img src ="http://www.cppblog.com/mysileng/aggbug/164611.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mysileng/" target="_blank">鑫龙</a> 2012-01-28 19:40 <a href="http://www.cppblog.com/mysileng/archive/2012/01/28/164611.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何将二维数组(静态的和动态的)作为函数的参数传递</title><link>http://www.cppblog.com/mysileng/archive/2012/01/28/164610.html</link><dc:creator>鑫龙</dc:creator><author>鑫龙</author><pubDate>Sat, 28 Jan 2012 11:24:00 GMT</pubDate><guid>http://www.cppblog.com/mysileng/archive/2012/01/28/164610.html</guid><wfw:comment>http://www.cppblog.com/mysileng/comments/164610.html</wfw:comment><comments>http://www.cppblog.com/mysileng/archive/2012/01/28/164610.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/mysileng/comments/commentRss/164610.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mysileng/services/trackbacks/164610.html</trackback:ping><description><![CDATA[<div><span style="color: #74bc2f; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; "><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">今天写程序的时候要用到二维数组作参数传给一个函数，我发现将二维数组作参数进行传递还不是想象得那么简单，但是最后我也解决了遇到的问题，所以这篇文章主要介绍如何处理二维数组当作参数传递的情况，希望大家不至于再在这上面浪费时间。</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 25px; ">下文是我从互联网上download的一篇文章，讲的很好，但是我后面将指出问题所在，并加以改进，希望对你有所帮助：</span><br style="line-height: 25px; " /><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">首先，我引用了谭浩强先生编著的《<span style="line-height: 34px; ">C</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">程序设计》上面的一节原文，它简要介绍了如何将二维数组作为参数传递，原文如下<span style="line-height: 34px; ">(</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">略有改变，请原谅<span style="line-height: 34px; ">)</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">：</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">[</span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">原文开始<span style="line-height: 34px; ">]</span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">可以用二维数组名作为实参或者形参，在被调用函数中对形参数组定义时可以指定所有维数的大小，<span style="line-height: 34px; ">也可以省略第一维的大小说明</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">，如：</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int array[3][10]);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int array[][10]);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">二者都是合法而且等价，<span style="line-height: 34px; ">但是不能把第二维或者更高维的大小省略</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">，如下面的定义是不合法的：</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int array[][]);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">因为从实参传递来的是数组的起始地址，在内存中按数组排列规则存放<span style="line-height: 34px; ">(</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">按行存放<span style="line-height: 34px; ">)</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">，而并不区分行和列，如果在形参中不说明列数，则系统无法决定应为多少行多少列，<span style="line-height: 34px; ">不能只指定一维而不指定第二维，下面写法是错误的：</span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int array[3][]);</span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">实参数组维数可以大于形参数组，例如实参数组定义为：</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int array[3][10]);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">而形参数组定义为：</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">int array[5][10];</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">这时形参数组只取实参数组的一部分，其余部分不起作用。</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">[</span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">原文结束<span style="line-height: 34px; ">]</span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">大家可以看到，将二维数组当作参数的时候，必须指明所有维数大小或者省略第一维的，但是不能省略第二维或者更高维的大小，这是由编译器原理限制的。大家在学编译原理这么课程的时候知道编译器是这样处理数组的：</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">对于数组<span style="line-height: 34px; ">&nbsp;int p[m][n];</span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">如果要取<span style="line-height: 34px; ">p[i][j]</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">的值<span style="line-height: 34px; ">(i&gt;=0 &amp;&amp; i&lt;m &amp;&amp; 0&lt;=j &amp;&amp; j &lt; n)</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">，编译器是这样寻址的，它的地址为：</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">p + i*n + j;</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">从以上可以看出，如果我们省略了第二维或者更高维的大小，编译器将不知道如何正确的寻址。但是我们在编写程序的时候却需要用到各个维数都不固定的二维数组作为参数，这就难办了，编译器不能识别阿，怎么办呢？不要着急，编译器虽然不能识别，但是我们完全可以不把它当作一个二维数组，而是把它当作一个普通的指针，再另外加上两个参数指明各个维数，然后我们为二维数组手工寻址，这样就达到了将二维数组作为函数的参数传递的目的，根据这个思想，我们可以把维数固定的参数变为维数随即的参数，例如：</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; void Func(int array[3][10]);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int array[][10]);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">变为：</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int **array, int m, int n);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">在转变后的函数中，<span style="line-height: 34px; ">array[i][j]</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">这样的式子是不对的<span style="line-height: 34px; ">(</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">不信，大家可以试一下<span style="line-height: 34px; ">)</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">，因为编译器不能正确的为它寻址，所以我们需要模仿编译器的行为把<span style="line-height: 34px; ">array[i][j]</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">这样的式子手工转变为：</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">*((int*)array + n*i + j);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">在调用这样的函数的时候，需要注意一下，如下面的例子：</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">int a[3][3] =</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{1, 1, 1},</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{2, 2, 2},</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{3, 3, 3}</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">};</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">Func(a, 3, 3);</span><br style="line-height: 25px; " /></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">根据不同编译器不同的设置，可能出现<span style="line-height: 34px; ">warning&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">或者<span style="line-height: 34px; ">error,</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">可以进行强制转换如下调用：<span style="line-height: 34px; ">&nbsp;</span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">Func((int**)a, 3, 3);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">其实多维数组和二维数组原理是一样的，大家可以自己扩充的多维数组，这里不再赘述。写到这里，我先向看了这篇文章后悔的人道歉，浪费你的时间了。下面是一个完整的例子程序，这个例子程序的主要功能是求一个图中某个顶点到其他顶点的最短路经，图是以邻接矩阵的形式存放的<span style="line-height: 34px; ">(</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">也就是一个二维数组<span style="line-height: 34px; ">)</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">，其实这个函数也是挺有用的，但是我们这篇文章的重点在于将二维数组作为函数的参数传递。</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp;<span style="line-height: 34px; ">&nbsp;上文结束，上文最后指出了实现二位数组作为函数参数的方法，但是它实现的是将静态的二位数组作为参数，但是如何将动态而为数组作为参数呢？上面的方法显然是不合适的，下面是我琢磨出来的方法。</span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; color: #ff0000; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 34px; ">先将静态数组作为参数的代码贴出来:</span></span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">#include &lt;iostream&gt;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">using namespace std;<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Calc(int **A, int m, int n);<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">int _tmain(int argc, _TCHAR* argv[])<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int row = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int col = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int i = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int A[3][3];<br style="line-height: 34px; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; for (row = 0; row &lt; 3; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (col = 0; col &lt; 3; col++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; A[row][col] = row + col;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " />&nbsp;&nbsp;&nbsp;<span style="line-height: 34px; ">&nbsp;Calc((int **)A, 3, 3);</span><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; return 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">}<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Calc(int **A, int m, int n)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; if (NULL == A || m &lt;1 || n &lt; 1)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int row = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int col = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int i = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int j = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int **matrix = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; matrix = new int*[m];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; if (NULL == matrix)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; for (row = 0; row &lt; m; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; matrix[row] = new int[n];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (NULL == matrix[row])<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (i = 0; i &lt; row; i++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; delete []matrix[i];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; matrix[i] = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; delete []matrix;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; matrix = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; for (row = 0; row &lt; m; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (col = 0; col &lt; n; col++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; color: #ff0000; " /><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; matrix[row][col] = *((int *)A + row * n + col);</span><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //matrix[row][col] = A[row][col];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 34px; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; for (row = 0; row &lt; m; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (col = 0; col &lt; n; col++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;matrix[row][col]&lt;&lt;"&nbsp; ";<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"\n";<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">}</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">下面是动态二位数组作为函数参数时的代码：</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">#include &lt;iostream&gt;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">using namespace std;<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Calc(int **A, int m, int n);<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">int main(int argc, char* argv[])<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int row = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int col = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int i = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int **A = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; A = new int*[3];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; if (NULL == A)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; for (row = 0; row &lt; 3; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; A[row] = new int[3];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (NULL == A[row])<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (i = 0; i &lt; row; i++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; delete []A[i];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; A[i] = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; delete []A;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; A = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; for (row = 0; row &lt; 3; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (col = 0; col &lt; 3; col++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; A[row][col] = row + col;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " />&nbsp;&nbsp;&nbsp;<span style="line-height: 34px; ">&nbsp;Calc((int **)A, 3, 3);</span><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; return 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">}<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Calc(int **A, int m, int n)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; if (NULL == A || m &lt;1 || n &lt; 1)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int row = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int col = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int i = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int j = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; int **matrix = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; matrix = new int*[m];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; if (NULL == matrix)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; for (row = 0; row &lt; m; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; matrix[row] = new int[n];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (NULL == matrix[row])<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (i = 0; i &lt; row; i++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; delete []matrix[i];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; matrix[i] = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; delete []matrix;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; matrix = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; for (row = 0; row &lt; m; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (col = 0; col &lt; n; col++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //matrix[row][col] = *((int *)A + row * n + col);<br style="line-height: 34px; " />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 34px; ">matrix[row][col] = A[row][col];</span><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 34px; " />&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; for (row = 0; row &lt; m; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (col = 0; col &lt; n; col++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;matrix[row][col]&lt;&lt;"&nbsp; ";<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout&lt;&lt;"\n";<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">&nbsp;&nbsp;&nbsp; }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">}<br style="line-height: 34px; " />&nbsp;&nbsp;&nbsp;<span style="line-height: 34px; ">&nbsp;注意上面的代码的不同之处，即将动态二维数组作为函数参数时，在函数里面应用时候要将其伪装成静态二维数组！</span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">&nbsp;&nbsp;&nbsp; 这样，以上的两段代码不仅实现了堆和栈之间数据的传递，而且实现了堆和堆之间数据的传递！</span></span></p></span></div><img src ="http://www.cppblog.com/mysileng/aggbug/164610.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mysileng/" target="_blank">鑫龙</a> 2012-01-28 19:24 <a href="http://www.cppblog.com/mysileng/archive/2012/01/28/164610.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TCPL学习笔记-----第6章------(结构) </title><link>http://www.cppblog.com/mysileng/archive/2011/09/09/155456.html</link><dc:creator>鑫龙</dc:creator><author>鑫龙</author><pubDate>Fri, 09 Sep 2011 07:39:00 GMT</pubDate><guid>http://www.cppblog.com/mysileng/archive/2011/09/09/155456.html</guid><wfw:comment>http://www.cppblog.com/mysileng/comments/155456.html</wfw:comment><comments>http://www.cppblog.com/mysileng/archive/2011/09/09/155456.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mysileng/comments/commentRss/155456.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mysileng/services/trackbacks/155456.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: 14px/21px verdana, 'courier new'; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong>6.1结构的基本知识<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: 14px/21px verdana, 'courier new'; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong>6.2结构与函数<br /></strong><span style="color: red">p114</span>---(1)不同的结构体变量之间进行赋值会进行简单的浅拷贝值传递。<br /><span style="color: red">p115</span>---(2)结构运算符"." 和"-&gt;",用于函数调用的"()"以及用于下标的"[]"的算术符优先级最高。<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: 14px/21px verdana, 'courier new'; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong>6.3结构数组<br /></strong><span style="color: red">p116</span>---(1)结构体数组初始化<br />&nbsp; struct key {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char&nbsp;*word;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp; count;<br />&nbsp; }keytab[] = {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"auto",0},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {"break",0},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"case",0},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"while",0}<br />&nbsp; };<br /><span style="color: red">p118</span>---(2)计算结构体数组中的结构体个数时，需要用到sizeof, sizeof keytab / sizeof(struct key) 。另外条件编译语句#if中不能使用sizeof，因为预处理器部队类型名进行分析.但预处理器并不计算#define语句中的表达式,因此在#define中使用sizeof是合法的。<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: 14px/21px verdana, 'courier new'; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong>6.4指向结构的指针<br /></strong><span style="color: red">p120</span>---(1)两个指针之间的加法运算是非法的。但是,指针的剑法运算却是合法的,mid=(low+high)/2是错误的,但是mid=low+(high-low)/2就可以了。<br /><span style="color: red">p115</span>---(2)千万不要以为结构的长度等于各成员长度的和。因为不同的对象有不同的对齐要求，所以，结构中可能会出现未命名的&#8220;空穴&#8221;。一般需要使用sizeof来正确获得结构体长度。<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: 14px/21px verdana, 'courier new'; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong>6.5自引用结构<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: 14px/21px verdana, 'courier new'; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong>6.6表查找<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: 14px/21px verdana, 'courier new'; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong>6.7类型定义(typedif)<br /></strong><span style="color: red">p127</span>---(1)typedef中声明的类型在变量位置出现，而不是紧接在关键字typedef之后。typedef在语法上类似与extern等。<br /><span style="color: red">p128</span>---(2)typedef类似与#define语句，但由于typedef是由编译器解释的，因此它的问题替换功能要超过预处理器的能力。例如: typedef int (*PFI)(char *,char*);<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: 14px/21px verdana, 'courier new'; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong>6.8联合<br /></strong><span style="color: red">p128</span>---(1)联合提供了一种方式，以在单块存储区中管理不同类型的数据，目的在于一个变量可以合法的保存多种数据类型中的任何一种类型。<br /><span style="color: red">p129</span>---(2)联合体变量读取的类型必须是最近一次存入的类型。程序员赋值跟踪当前保存在联合中的类型，如果保存的类型和读取的类型不一致，其结果取决于具体的实现。<br /><span style="color: red">p130</span>---(3)联合就是一个结构，他得所有成员相对于基地址的偏移量都是0，此结构空间要大到足够容纳最&#8220;宽&#8221;的成员。<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: 14px/21px verdana, 'courier new'; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong>6.9位字段<br /></strong><span style="color: red">p131</span>---(1)struct {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned int is_keyword : 1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned int is_extern&nbsp;&nbsp;&nbsp;: 1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }flag;<br />这里定义了一个变量flag，它包含3个1位的字段。冒号后的数字表示字段的宽带(2进制)。<br />flag.is_keyword = 1;</span></span></span></strong></span></strong></span></span></span></span></strong></span><img src ="http://www.cppblog.com/mysileng/aggbug/155456.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mysileng/" target="_blank">鑫龙</a> 2011-09-09 15:39 <a href="http://www.cppblog.com/mysileng/archive/2011/09/09/155456.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TCPL学习笔记-----第5章------(指针与数组)</title><link>http://www.cppblog.com/mysileng/archive/2011/07/21/151553.html</link><dc:creator>鑫龙</dc:creator><author>鑫龙</author><pubDate>Thu, 21 Jul 2011 08:07:00 GMT</pubDate><guid>http://www.cppblog.com/mysileng/archive/2011/07/21/151553.html</guid><wfw:comment>http://www.cppblog.com/mysileng/comments/151553.html</wfw:comment><comments>http://www.cppblog.com/mysileng/archive/2011/07/21/151553.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mysileng/comments/commentRss/151553.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mysileng/services/trackbacks/151553.html</trackback:ping><description><![CDATA[<p><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><strong>5.1指针与地址<br /></strong><span style="color: red">p79</span>---(1)指针是能够存放一个地址的一组存储单元，通常是两个或者四个字节。<br /><span style="color: red">p80</span>---(2)int *ip ，该声明语句表明*ip的结果是一个int类型。<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><strong>5.2指针与函数参数<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><strong>5.3指针与数组<br /></strong><span style="color: red">p84</span>---(1)如果pa指向数组中的某个特定元素，那么根据指针运算的定义，pa+1将指向下一个元素，pa+i将指向pa所指向数组元素之后的第i个元素。例如*(pa+i)等同于a[i]。<br /><span style="color: red">p85</span>---(2)我们必须记住，数组名和指针之间有一个不同之处。指针是一个变量，因此，在c语言中，语句pa=a和pa++都是合法的。但数组名不是变量，因此，类似于a=pa，a++的语句都是非法的。<br /><font size="2"><span style="color: red">p86</span>---(3)如果确信相应的元素存在，也可以通过下标访问数组第一个元素之前的元素。类似：p[-1]、p[-2]这样的表达式在语法上也是合法的，它们分别引用位于p[0]之前的两个元素，当然引用数组边界之外的对象是非法的。<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><strong>5.4地址算术运算<br /></strong><span style="color: red">p87</span>---(1)c语言保证，0永远不是有效的数据地址，因此返回值0可用来表示发生了异常事件。<br /><span style="color: red">p88</span>---(2)指针与整数之间不能相互转换，但0是惟一的例外：常量0可以赋值给指针，指针也可以和常数0进行比较。程序中经常用符号常量NULL代替常量0，这样便于更清晰地说明常量0是指针的一个特殊值。符号常量NULL定义在标准头文件&lt;stddef.h&gt;中。<br /><font size="2"><span style="color: red">p89</span>---(3)有效的指针运算包括相同类型指针之间的赋值运算；指针同整数之间的加法或减法运算；指向相同数组中元素的两个指针间的减法或比较运算，将指针赋值为0或指针与0之间的比较运算。其他所有形式的指针运算都是非法的，例如两个指针间的加法，乘法，除法，移位或屏蔽运算；指针同float或double类型之间的加法运算；不经强制类型转换而直接将指向一种类型对象的指针赋值给指向另一种类型对象的指针运算。<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><strong>5.5字符指针与函数<br /></strong><span style="color: red">p90</span>---(1)注意char amessage[] = "now"; 和char *pmessage = "now";的区别，amesaage是一个仅仅存放初始化字符串以及空字符'\0'的一维数组。它始终指向同一个存储位置。而pmeesage是一个指针，其初值指向一个字符串常量，之后还可以被修改以指向其他地址。<br /><br /><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><strong>5.6指针数组以及指向指针的指针<br /></strong><span style="color: red">p94</span>---(1)char *lineptr[MAXLINES]是一个指针数组，lineptr[i]是一个字符指针，而*lineptr[i]是该指针指向的第i个文本行的首字符。<br /></span></span></span></span></font></span></span></font></span></span></strong></span></span></span></span></p><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><span style="background-color: #ffffff"><span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; widows: 2; orphans: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff">
<p><br /><strong>5.7多维数组<br /></strong><span>p95</span><span>---(1)char daytab[2][3] =&nbsp; {{1,2,3},{1,2,3}};</span><br /><span>p96</span><span>---(2)如果将二维数组作为参数传递给函数，那么在函数的参数声明中必须指明数组的列数。</span><br /><span>p97</span><span>---(</span><font size="2"><font size="2"><font size="2"><font size="2">3)<span style="background-color: yellow">特别注意，方括号[]的优先级高于*的优先级，所以int (*day)[13] 与 int *day[13]是完全不同的含义。前者是表示一个二维数组，每个元素放int类型；后者表示一维指针数组，里面放的是指向一个int类型的地址。</span></font></font></font></font><font size="2"><font size="2"><font size="2"><font size="2"><span><font size="2"><font size="2"><font size="2"><font size="2"><span><!--EndFragment--></span></font></font></font></font></p>
<p class="p0" style="margin-top: 3.75pt; margin-bottom: 3.75pt"><span style="font-weight: bold; font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><br />5.8</span><span style="font-weight: bold; font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">指针数组的初始化</span><span style="font-weight: bold; font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 3.75pt; margin-bottom: 3.75pt"><span style="font-weight: bold; font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><br />5.9<font face="宋体">指针与多维数组</font></span><span style="font-weight: bold; font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 3.75pt; margin-bottom: 3.75pt"><span style="font-size: 10.5pt; color: rgb(255,0,0); font-family: 'Verdana'; mso-spacerun: 'yes'">p98</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">---(1)</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">到目前为止，指针数组最频繁的用处是存放具有不同长度的字符串</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 3.75pt; margin-bottom: 3.75pt"><span style="font-weight: bold; font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><br />5.10<font face="宋体">命令行参数</font></span><span style="font-weight: bold; font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 3.75pt; margin-bottom: 3.75pt"><span style="font-size: 10.5pt; color: rgb(255,0,0); font-family: 'Verdana'; mso-spacerun: 'yes'">p98</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">---(1)</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">调用主函数</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">main</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">时，它有两个参数。第一个参数</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">argc</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">的值表示运行程序时命令行中参数的数目；第二个参数</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">argv</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">是一个指向字符串数组的指针，其中每一个字符串对应一个参数。</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 3.75pt; margin-bottom: 3.75pt"><span style="font-size: 10.5pt; color: rgb(255,0,0); font-family: 'Verdana'; mso-spacerun: 'yes'">p99</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">---(2)<font face="宋体">按照</font><font face="Verdana">c</font><font face="宋体">语言的约定，</font><font face="Verdana">argv[0]</font><font face="宋体">的值是启动程序的程序名，因此</font><font face="Verdana">argc</font><font face="宋体">的值至少为</font><font face="Verdana">1.</font><font face="宋体">如果</font><font face="Verdana">argc</font><font face="宋体">的值为</font><font face="Verdana">1</font><font face="宋体">，则说明程序后面没有命令行参数。第一个可选的参数为</font><font face="Verdana">argv[1],</font><font face="宋体">而最后一个可选参数为</font><font face="Verdana">argv[argc-1]</font><font face="宋体">。另外，</font><font face="Verdana">ANSI</font><font face="宋体">标准要求</font><font face="Verdana">argv[argc]</font><font face="宋体">必须为一个空指针。</font></span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 3.75pt; margin-bottom: 3.75pt"><span style="font-size: 10.5pt; color: rgb(255,0,0); font-family: 'Verdana'; mso-spacerun: 'yes'">p101</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">---(3)<font face="宋体">注意</font><font face="Verdana">(*++argv)[0]</font><font face="宋体">与</font><font face="Verdana">*++argv[0]</font><font face="宋体">的区别，前者是行移动，后者是列移动。</font></span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">&nbsp;</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 3.75pt; margin-bottom: 3.75pt"><span style="font-weight: bold; font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">5.11<font face="宋体">指向函数的指针</font></span><span style="font-weight: bold; font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 3.75pt; margin-bottom: 3.75pt"><span style="font-size: 10.5pt; color: rgb(255,0,0); font-family: 'Verdana'; mso-spacerun: 'yes'">p104</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">---(1)<font face="宋体">注意</font><font face="Verdana">int&nbsp;(*comp)(void&nbsp;*,void&nbsp;*)</font><font face="宋体">与</font><font face="Verdana">int&nbsp;*comp(void&nbsp;*,void&nbsp;*)</font><font face="宋体">的区别，前者</font><font face="Verdana">comp</font><font face="宋体">是一个函数指针指向一个返回类型为</font><font face="Verdana">int</font><font face="宋体">的函数，而后者</font><font face="Verdana">comp</font><font face="宋体">为函数名返回类型为</font><font face="Verdana">int</font><font face="宋体">指针。</font></span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 3.75pt; margin-bottom: 3.75pt"><span style="font-weight: bold; font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><br />5.12<font face="宋体">复杂声明（这节后面的程序有点复杂，直接无视，哈哈</font><font face="Verdana">~~</font><font face="宋体">）&nbsp;</font></span><span style="font-weight: bold; font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 3.75pt; margin-bottom: 3.75pt"><span style="font-size: 10.5pt; color: rgb(255,0,0); font-family: 'Verdana'; mso-spacerun: 'yes'">p106</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">---(1)char&nbsp;(*(*x())[])()&nbsp;,x<font face="宋体">是一个无参函数，返回一个指针，指针指向一个数组，数组中的元素也是指针，指向一个返回类型为</font><font face="Verdana">char</font><font face="宋体">的无参函数。</font></span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; color: rgb(255,0,0); font-family: 'Verdana'; mso-spacerun: 'yes'">p106</span><span style="font-size: 10.5pt; font-family: 'Verdana'; mso-spacerun: 'yes'">---(2)char&nbsp;(*(*x[3])())[5]&nbsp;,x<font face="宋体">是一个长度为</font><font face="Verdana">3</font><font face="宋体">的数组，数组中的元素是指针，指向一个无参的函数，该函数返回类型也是指针，指向一个长度为</font><font face="Verdana">5</font><font face="宋体">的</font><font face="Verdana">char</font><font face="宋体">类型数组。</font></span></p></span></span></span></span></font></font></span></span></span></span></span></span></font></span></span></font></span></span></span></span></span></span><img src ="http://www.cppblog.com/mysileng/aggbug/151553.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mysileng/" target="_blank">鑫龙</a> 2011-07-21 16:07 <a href="http://www.cppblog.com/mysileng/archive/2011/07/21/151553.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TCPL学习笔记-----第4章------(函数与程序结构)</title><link>http://www.cppblog.com/mysileng/archive/2011/06/07/148207.html</link><dc:creator>鑫龙</dc:creator><author>鑫龙</author><pubDate>Tue, 07 Jun 2011 07:47:00 GMT</pubDate><guid>http://www.cppblog.com/mysileng/archive/2011/06/07/148207.html</guid><wfw:comment>http://www.cppblog.com/mysileng/comments/148207.html</wfw:comment><comments>http://www.cppblog.com/mysileng/archive/2011/06/07/148207.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mysileng/comments/commentRss/148207.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mysileng/services/trackbacks/148207.html</trackback:ping><description><![CDATA[<div><strong>4.1函数的基本知识<br /></strong><span style="color: red">p59</span>---(1)如果函数定义中省略了返回类型，则默认为int类型。函数之间的通信可以通过参数、函数返回值、外部变量进行。<br /><span style="color: red">p60</span>---(2)假定有三个函数分别存放在名为main.c、getline.c、strindex.c的三个文件中，则可以使用命令：cc main.c getline.c strindex.c 来编译这3个文件，并把生成的目标代码分别存放在文件main.o、getline.o、strindex.o中，然后再把这3个文件一起加载到可执行文件a.out中。如果源文件中存在错误（比如文件main.c中存在错误）,则可以通过命令:cc main.c getline.o strindex.o 对main.c重新编译，并将编译的结果与以前已编译过的目标文件getline.o、strindex.o一起加载到可执行文件中。cc命令使用".c"和".o"这两个拓展名来区分源文件和目标文件。<br /><br /><strong>4.2返回非整型值的函数<br /></strong><span style="color: red">p61</span>---(1)函数的声明与定义必须一致。如果函数与调用它的主函数main放在同一个源文件中，并且类型不一致，编译器就会检测到该错误。但是如果函数是单独编译的，这种不匹配的错误就无法检测出来，函数将返回非int类型的值，而main函数却将返回值按照int类型处理，最后结果将变得毫无意义。实验如下：<br />test1.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test2.c<br />#include&lt;stdio.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #include&lt;stdio.h&gt;<br />double f(){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void main(){<br />&nbsp;return 1.1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int i = f();<br />}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d\n",i);&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />通过cc test1.c test2.c编译链接生成a.out,运行i的结果是毫无意义的。(结果并不是1,不知道为什么。。。)<br /><br /><strong>4.3外部变量<br /></strong><span style="color: red">p66</span>---(1)由于程序要超前读入一个字符，这样就导致最后有一个字符不属于当前所要读入的数。如果能&#8220;反读&#8221;不需要的字符，该问题就可以得到解决。标准库提供了函数ungetc,它将一个字符压回到缓冲中。<br /><br /><strong>4.4作用域规则<br /></strong><span style="color: red">p68</span>---(1)对于在函数开头声明的自动变量来说。其作用域是声明该变量名的函数，不同函数中声明的具有相同名字的各个局部变量之间没有任何关系。外部变量的作用域从声明它的地方开始，到其所在的文件(待编译)的末尾结束。<br /><span style="color: red">p68</span>---(2)<span style="background-color: yellow">将外部变量的声明与定义严格区分开来很重要。变量声明用于说明变量的属性(主要是类型)，而变量定义除此之外还将引起存储器的分配。通常局部变量和不加extern的外部变量是集声明与定义于一体的。</span><br /><span style="color: red">p69</span>---(3)在一个源程序的所有源文件中，一个外部变量只能在某个文件中定义一次，而其他文件可以通过extern声明来访问。外部变量的定义中必须制定数组的长度，但extern声明则不一定要制定数组的长度。<br /><br /><strong>4.5头文件<br /></strong><span style="color: red">p70</span>---(1)我们对下面两个因素进行了折中：一方面是我们期望每个文件只能访问它完成任务所需要的信息；另一个方面是现实中维护较多的头文件比较困难。我们可以得出这样一个结论：对于某些中等规模的程序，最好只用一个头文件存放程序中各部分共享的对象。较大的程序需要使用更多的头文件，我们需要精心组织它们。<br /><br /><strong>4.6静态变量<br /></strong><span style="color: red">p70</span>---(1)用static声明来限定外部变量与函数，可以将其后声明的对象的作用域限制为被编译源文件的剩余部分，以达到对外隐藏的目的。<br /><span style="color: red">p71</span>---(2)static也可以用来声明内部变量，static类型的内部变量同自动变量一样，是某个特定函数的局部变量，只能在该函数中使用，但它与自动变量不同的是，不管其所在函数是否被调用，它一直存在，而不像自动变量那样，随着所在函数的被调用和退出而存在和消失。换句话说，static类型的内部变量是一种只能在某个特定函数中使用但一直占据存储空间的变量。<br /><br /><strong>4.7寄存器变量<br /></strong><span style="color: red">p71</span>---(1)register声明只适用于自动变量以及函数的形式参数。它所声明的变量在程序中使用频率较高，思想是将register变量放在机器的寄存器中。<br /><span style="color: red">p71</span>---(2)无论寄存器变量实际上是不是存放在寄存器中，它的地址都是不能访问的。在不噢那个的机器中，对寄存器变量的数目和类型的具体限制也是不同的。<br /><strong>4.8程序块结构<br /><br /></strong>
<p><strong>4.9初始化<br /></strong><span style="color: red">p72</span>---(1)在不显式初始化的情况下，外部变量和静态变量都将被隐式初始化为0，而自动变量和寄存器变量的初值则没有意义。<br /><span style="color: red">p72</span>---(2)外部变量与静态变量来说，初始化的表达式必须是常量表达式(即不能是之前已经定义过的值)。<br /></p>
<p><strong>4.10递归<br /><br /></strong></p>
<p><strong>4.11C预处理器<br />4.11.1文件包含<br /></strong><span style="color: red">p75</span>---(1)#include的行将被替换为由文件名指定的文件的内容。如果文件名用引号引起来，则在源文件所在位置查找该文件；如果没有找到文件，或者使用尖括号把文件名引起来，则将根据相应的规则 查找该文件，这个规则同具体的实现有关。<br /></p>
<p><strong>4.11.2宏替换<br /></strong><span style="color: red">p76</span>---(1)通常情况下，#define指定占一行，替换文本是#define指定行尾部的所有剩余部分内容，但也可以把一个较长的宏定义分成若干行，这时需要在待续的行末尾加上一个反斜杠符\。<br /><span style="color: red">p76</span>---(2)宏定义也可以带参数，如define max(A,B) ((A)&gt;(B)?(A):(B)),调用时候只需要x = max(i,j)就可以。但仔细考虑一下max的展开式，就会发现存在一些缺陷，其中表达式要重复计算2次，那么假如这样调用x = max(i++,j++)结果会使i或j自增两次。<br /><span style="color: red">p77</span>---(3)预处理器运算符##提供了一种连接实际参数的手段。如果替换文本中的参数与##相邻，则该参数将被实际参数替换，##与前后的空白符将被删除，并对替换后的结果重新扫描。例如 #define paster(f,b) f##b ，调用的时候paster(i,5)结果将是i5的参数。<br /></p>
<p><strong>4.11.3条件包含<br /></strong><span style="color: red">p78</span>---(1)#if语句对其中的常量整形表达式（不能包含sizeof、类型转换、enum常量）进行求值，若表达式的值非0则包含其后各行，直到遇到#endif\#elif或#else语句为止。<br />还有其他的#ifdef、#ifndef、#endif等等。。。。</p></div> <img src ="http://www.cppblog.com/mysileng/aggbug/148207.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mysileng/" target="_blank">鑫龙</a> 2011-06-07 15:47 <a href="http://www.cppblog.com/mysileng/archive/2011/06/07/148207.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TCPL学习笔记-----第3章------(控制流) </title><link>http://www.cppblog.com/mysileng/archive/2011/05/07/145891.html</link><dc:creator>鑫龙</dc:creator><author>鑫龙</author><pubDate>Sat, 07 May 2011 05:38:00 GMT</pubDate><guid>http://www.cppblog.com/mysileng/archive/2011/05/07/145891.html</guid><wfw:comment>http://www.cppblog.com/mysileng/comments/145891.html</wfw:comment><comments>http://www.cppblog.com/mysileng/archive/2011/05/07/145891.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mysileng/comments/commentRss/145891.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mysileng/services/trackbacks/145891.html</trackback:ping><description><![CDATA[<strong>3.1语句与程序块<br></strong><span style="COLOR: red">p46</span>---(1)右花括号用于结束程序块，其后不需要分号。<br><br><strong>3.2if-else语句<br></strong><br><strong>3.3else-if语句<br></strong><br><strong>3.4switch语句<br></strong><br><strong>3.5while循环与for循环<br></strong><br><strong>3.6do-while循环<br></strong><br><strong>3.7break语句与continue语句<br><br>3.8goto语句与标号<br></strong><span style="COLOR: red">p54</span>---(1)从理论上上，goto语句是没有必要的。但是，在某些场合下goto语句还是用得着的。最常见的用法就是终止程序在某些深度嵌套的结构中的处理过程，例如一次跳出两层或多层循环。这种情况使用break是不能达到目的的，它只能从最内层循环退出到上一级的循环。<br><br><strong>以下是习题部分：<br></strong><span style="COLOR: red">1.</span>编写函数itoa(n,s),将整数n转化为字符串，包括负数。<br>&nbsp;<img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/mysileng/3-1.jpg" width=359 height=474>
<img src ="http://www.cppblog.com/mysileng/aggbug/145891.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mysileng/" target="_blank">鑫龙</a> 2011-05-07 13:38 <a href="http://www.cppblog.com/mysileng/archive/2011/05/07/145891.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TCPL学习笔记-----第2章------(类型、运算符与表达式)</title><link>http://www.cppblog.com/mysileng/archive/2011/05/05/145770.html</link><dc:creator>鑫龙</dc:creator><author>鑫龙</author><pubDate>Thu, 05 May 2011 12:35:00 GMT</pubDate><guid>http://www.cppblog.com/mysileng/archive/2011/05/05/145770.html</guid><wfw:comment>http://www.cppblog.com/mysileng/comments/145770.html</wfw:comment><comments>http://www.cppblog.com/mysileng/archive/2011/05/05/145770.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mysileng/comments/commentRss/145770.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mysileng/services/trackbacks/145770.html</trackback:ping><description><![CDATA[<p><strong>2.1变量名<br /></strong><span style="color: red">p27</span>---(1)对变量的命名与符号常量的命令存在一些限制条件。名字由字母和数字组成的序列，但其第一个字符必须为字母。下划线"_"被看成是字母，通常用于命名较长的变量名，以提高其可读性。由于库例程的名字通常以下划线开头，因此变量名不要以下划线开头。<br /><br /><strong>2.2数据类型及长度<br /></strong><span style="color: red">p28</span>---(1)short类型通常为16位，long类型通常为32位，int类型可以是16位或者32位。各编译器可以根据硬件特性自主选择合适的类型长度，但要遵守下列限制：short与int类型至少是16位，而long类型至少为32位，并且short类型不得长于int类型，而int类型不得长于long类型。</p><br /><strong>2.3常量<br /></strong><span style="color: red">p28</span>---(1)常量分为整数常量、浮点数常量、字符串常量、字符常量以及枚举常量。其中字符常量还可以使用转义字符序列代替。<br /><span style="color: red">p28</span>---(2)默认的普通整数常量是int类型，浮点数常量时double类型。当整数常量超过int类型的范围会自动被当做long类型处理。如果不超过范围时候以字母l结尾也将被当成long类型常量。若后缀是f代表floeat型。若后缀是u代表unsigned无符号数。（ul代表unsigned long）<br /><span style="color: red">p31</span>---(3)枚举类型格式: enum boolean {NO=0,YES};想对于#define语句来说，它的优势常量值可以自动生成，而且使用变量存储这些值得时候，枚举变量还提供是否为该枚举类型的有效值的检查，因此枚举比#define更具优势。<br /><br /><strong>2.4声明<br /></strong><span style="color: red">p31</span>---(1)所谓的声明就是extern int a;但是编译器并不会为a分配内存即不会压入栈中，只有当int a =1；被执行及初始化或者叫定义以后才会被分配内存。(通常认为int a=1集声明与定义于一体)。<br /><span style="color: red">p31</span>---(2)未经显式初始化的变量的值为未定义值即无效值。<br /><span style="color: red">p31</span>---(3)任何变量的声明都可以使用const限定符限定。该限定符指定变量的值不能被修改。对数组而言，const限定符指定数组所有元素的值都不能被修改。const限定符也可以配合数组参数使用，它表明函数不能修改数组元素的值。<br /><br /><strong>2.5算术运算符<br /></strong><span style="color: red">p32</span>---(1)取模运算符%不能应用于float或double类型。在有负操作数的情况下，整数除法截取的方向以及取模运算结果的符号取决于具体机器的实现。<br /><br /><strong>2.6关系运算符与逻辑运算符<br /></strong><br /><strong>2.7类型转换<br /></strong><span style="color: red">p33</span>---(1)自动转换是指把比较窄的操作数转换成比较宽的操作数，并且不丢失信息的转换。而如果把比较宽的操作数转换成比较窄的操作数通常需要用到强制类型转换。<br /><span style="color: red">p34</span>---(2)C语言定义保证了机器的标准打印字符集中的字符不会是负值，因此，在表达式中这些字符总是正值。但是，存储在字符变量中的值理论上是可以为正也可以为负的。为了保证程序的可移植性，如果要在char类型的变量中存储非字符数据，最好指定signed或者unsigned限定符。<br /><span style="color: red">p35</span>---(3)一般来说，如果二元运算符的两个操作数具有不同的类型，那么在进行运算之前先把&#8220;较低&#8221;的类型提升为&#8220;较高&#8221;的类型。且运算结果是&#8220;较高&#8221;的类型。但是如果两个操作数中包含unsigned类型的操作数时，转换规则要复杂一些，因为带符号值与无符号值之间的比较运算是与机器有关的。例如-1L与1U比较大小，如果把两者都提升为有符号数则后者大，如果把两者都提升为无符号的数则前者较大。<br /><br /><strong>2.8自增运算符与自减运算符<br /></strong><br /><strong>2.9按位运算符<br /></strong><span style="color: red">p38</span>---(1)C语言提供了6个位操作运算符。这些运算符只能作用于整数操作数，即有符号或者无符号的char\short\int\long。6种分别是:&amp;、|、^、~、&gt;&gt;、&lt;&lt;。<br /><span style="color: red">p39</span>---(2)对于&gt;&gt;左移时，有符号数高位用符号位填补、无符号数高位用0填补。对于&lt;&lt;右移时，一律用0填补。<br /><br /><strong>2.10赋值运算符与表达式<br /></strong><br /><strong>2.11条件表达式<br /></strong><br /><strong>2.12运算符优先级与求值次序<br /></strong><br /><br /><strong>以下是练习题部分：<br /></strong><span style="color: red">1.</span> 编写一个程序以确定分别由signed及unsigned限定的char、short、int、long类型变量的取值范围。<br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/mysileng/2-1.jpg" width="786" height="312" /><br /><span style="color: red">2.</span>编写函数htoi(s),把由十六进制数字组成的字符串转为为与之等价的整型值。<br /><span style="color: red"><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/mysileng/2-3.jpg" width="544" height="494" /><br />3.</span>编写一个函数setbits(x,p,n,y)，该函数返回对x执行下列操作后的结果值：将x中从第p为开始的n个位设置成y中最右边n位的值，x其余各位保持不变。 <br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/mysileng/2-6.jpg" width="393" height="363" /> <img src ="http://www.cppblog.com/mysileng/aggbug/145770.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mysileng/" target="_blank">鑫龙</a> 2011-05-05 20:35 <a href="http://www.cppblog.com/mysileng/archive/2011/05/05/145770.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TCPL学习笔记-----第1章------(导言)</title><link>http://www.cppblog.com/mysileng/archive/2011/05/03/145606.html</link><dc:creator>鑫龙</dc:creator><author>鑫龙</author><pubDate>Tue, 03 May 2011 14:29:00 GMT</pubDate><guid>http://www.cppblog.com/mysileng/archive/2011/05/03/145606.html</guid><wfw:comment>http://www.cppblog.com/mysileng/comments/145606.html</wfw:comment><comments>http://www.cppblog.com/mysileng/archive/2011/05/03/145606.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mysileng/comments/commentRss/145606.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mysileng/services/trackbacks/145606.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;TCPL就是the c programming language,现在想从头温习一遍C语言，先从这本教材开始吧。以后以每一章节为单位，我会摘抄一些书上的原话和写一些自己的总结与认识。最后会做一下这章的习题。（前提是如果做的出来的话）<br><strong>1.1入门<br></strong><span style="COLOR: red">p3</span>---(1)请注意，\n只代表一个字符。类似于\n的转义字符序列为表示无法输入的字符或不可见字符提供一种通用的可扩充机制。例如:\t表示制表符、\b表示回退符、\"表示双引号、\\表示反斜杠、\n表示换行符、\r表示回车符 等......<br><br><strong>1.2变量与算术表达式<br></strong><span style="COLOR: red">p4</span>---(1)在C语言中，所有变量都必须先声明后使用。声明通常放在函数的起始处，在任何可执行语句之前。<br><span style="COLOR: red">p4</span>---(2)对于int类型而言，通常为16位，其取值范围为-32768~32767之间，也有用32位表示的int类型。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/mysileng/p4.jpg" width=410 height=149><br><span style="COLOR: red">p5</span>---(3)char类型是<span style="COLOR: red">一个字节</span>。整数除法操作将执行舍位，结果中的任何小数部分都会被舍弃。<br><span style="COLOR: red">p6</span>---(4)printf函数并不是C语言本身的一部分，C语言本身并没有定义输入输出功能。printf仅仅是标准函数库中一个有用的函数而已。其中%d表示整数、%s表示字符串、%o表示八进制、%x表示十六进制数、%c表示字符、%%表示百分号本身。如果在%后面加数字，表示打印宽度（打印的内容会在打印区域右对齐）。特别的是%f表示浮点数,如果有小数点和数字的并列的话，小数点后的数字代表小数点后的显示精度，如:%6.2f。<br><strong>1.3for语句<br><br>1.4符号常量<br></strong><span style="COLOR: red">p9</span>---(1)#define 名字 替换文本。注意替换文本可以是任何字符序列，而不仅限于数字。当然此指令的末尾没有分号<br><br><strong>1.5字符输入/输出<br></strong><span style="COLOR: red">p9</span>---(1)标准库提供的输入输出模型非常简单。无论是从何处输入，输出到何处，其输入输出都是按照<span style="COLOR: red">字符流</span>的方式处理。文本流是由多行字符构成的字符序列，而每行字符则由0个或者多个字符构成，行末是一个换行符(\n)。<br><br><strong>1.5.1文件复制<br></strong><span style="COLOR: red">p10</span>---(1)因为某些潜在的重要原因，我们在此使用int类型来接受getchar()的返回值。之所以不把其用char类型装载是因为它必须足够大，除了能存储任何可能的字符外还要能存储文件的结束符EOF。<br><br><strong>1.5.2字符计数<br></strong><br><strong>1.5.3行计数<br></strong><br><strong>1.5.4单词计数<br></strong><br><strong>1.6数组<br></strong><br><strong>1.7函数<br></strong><span style="COLOR: red">p18</span>---(1)函数定义可以以任意次序出现在一个源文件或多个源文件中，但同一个函数不能分割存放在多个文件中。如果源程序分割分散在多个文件中，那么，在编译和加载时，就需要做更多的工作，但这是操作系统的原因，并不是语言的属性决定的。<br><span style="COLOR: red">p18</span>---(2)main函数的末尾有一个return语句。由于main本身也是函数，因此也可以向其调用者返回一个值，该调用者实际上就是程序的执行环境。一般来说，返回值为0表示正常终止，返回值为非0表示出现异常情况或出错结束条件。<br><br><strong>1.8参数——传值调用<br></strong><span style="COLOR: red">p19</span>---(1)在C语言中，所有函数参数都是&#8220;通过值&#8221;传递的。也就是说，传递给被调用函数的参数值存放在临时变量中，而不是存放在原来的变量中。<br><span style="COLOR: red">p20</span>---(2)必要时，也可以让函数能够修改主调用函数中的变量。这种情况下，调用者需要向被调用函数提供待设置值得变量的地址（也就是指针）。<br><br><strong>1.9字符数组<br></strong><span style="COLOR: red">p22</span>---(1)当在C语言程序中出现类似于"hello\n"的字符串常量时，它将以<span style="COLOR: red">字符数组的形式</span>存储，数组的各元素分别存储字符串的各个字符，并以'\0'标示字符串结束。（'\0'并不是普通文本的一部分，也就是'\0'是字符串常量一部分，你需要给它分配空间，但它并不是字符串内容的一部分，所以计算字符串长度时不会计算它）。<br><br><strong>1.10外部变量与作用域<br></strong><span style="COLOR: red">p24</span>---(1)函数在使用外部变量之前，必须要知道外部变量的名字。要达到该目的，第一种方式是在函数中使用extern类型的声明，这种类型的声明除了在前面加了一个关键字extern外，其他方面与普通变量的声明相同。另一种方式，在源文件中，如果外部变量的定义出现在使用它的函数之前，那么在那个函数中就没有必要使用extern声明。<br><span style="COLOR: red">p24</span>---(2)如果程序包含在多个源文件中，而某个变量在file1文件中定义、在file2和file3文件中使用，那么在文件file2与file3中就需要使用extern声明来建立该变量与其定义之间的联系。<br><br><strong>以下是练习题部分：<br></strong><span style="COLOR: red">1.</span>练习1-9 编写一个输入复制到输出的程序，并将其中连续的多个空格用一个空格代替<br><img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/mysileng/1-9.jpg" width=511 height=180><br><br><span style="COLOR: red">2.</span>练习1-11 简单的单词计数程序<br><img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/mysileng/1-11.jpg" width=463 height=377><br><br><span style="COLOR: red">3.</span>1-19 编写一个翻转字符串函数<br><img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/mysileng/1-19.jpg" width=398 height=376> 
<img src ="http://www.cppblog.com/mysileng/aggbug/145606.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mysileng/" target="_blank">鑫龙</a> 2011-05-03 22:29 <a href="http://www.cppblog.com/mysileng/archive/2011/05/03/145606.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>