﻿<?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++博客-刘加加在成长(C语言学习中)-随笔分类-学习C语言</title><link>http://www.cppblog.com/liujiajia/category/7032.html</link><description>学习、工作、生活。这是三个问题。</description><language>zh-cn</language><lastBuildDate>Fri, 30 May 2008 16:01:53 GMT</lastBuildDate><pubDate>Fri, 30 May 2008 16:01:53 GMT</pubDate><ttl>60</ttl><item><title>sprint() 格式化写入字符串</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/31/51655.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Fri, 30 May 2008 16:01:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/31/51655.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51655.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/31/51655.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51655.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51655.html</trackback:ping><description><![CDATA[<p>程序从读取用户输入的firstname,lastname和prize,并将他们读取的值按照一定格式存储到另一个字符串数组内。</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;MAX&nbsp;20</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;first[MAX];<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;last[MAX];<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;formal[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">MAX</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">];<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;prize;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Enter&nbsp;your&nbsp;first&nbsp;name</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;gets(first);<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Enter&nbsp;your&nbsp;last&nbsp;name</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;gets(last);<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Enter&nbsp;your&nbsp;prize&nbsp;money</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%lf</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">prize);<br>&nbsp;&nbsp;&nbsp;&nbsp;<strong>sprintf</strong>(formal,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s,%-19s:$%6.2f\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,last,first,prize);<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(formal);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}</span></div>
<img src ="http://www.cppblog.com/liujiajia/aggbug/51655.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-31 00:01 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/31/51655.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>strcpy() 复制字符串</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/30/51654.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Fri, 30 May 2008 15:57:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/30/51654.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51654.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/30/51654.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51654.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51654.html</trackback:ping><description><![CDATA[下面代码演示了程序循环从用户输入读取字符串，并存入临时的字符串数组，然后将临时字符串复制到qwords数组里面。<br><br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;SIZE&nbsp;40</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;LIM&nbsp;5</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;qwords[LIM][SIZE];<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;temp[SIZE];<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: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">LIM&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;gets(temp))<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>strcpy</strong>(qwords[i],temp);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">The&nbsp;list</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">LIM;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts(qwords[i]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}</span></div>
<img src ="http://www.cppblog.com/liujiajia/aggbug/51654.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-30 23:57 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/30/51654.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>strcmp() 比较字符串的内容</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/30/51653.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Fri, 30 May 2008 15:48:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/30/51653.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51653.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/30/51653.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51653.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51653.html</trackback:ping><description><![CDATA[下面程序使用strcmp()验证答案是否正确：<br><br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">[10];<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Who&nbsp;is&nbsp;1+1?</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;gets(</span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(<strong>strcmp</strong>(</span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">,"2")</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">No,it's&nbsp;wrong.Try&nbsp;again.</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gets(</span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">OK,That's&nbsp;right</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);</span></div>
<br><br>下面程序说明各种情况下，strcmp()的返回值：<br><br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">strcmp(\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,strcmp(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">strcmp(\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">b\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,strcmp(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">b</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">strcmp(\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">b\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,strcmp(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">b</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">strcmp(\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">c\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,strcmp(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">c</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">strcmp(\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">c\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,strcmp(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">c</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">strcmp(\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abc\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abd\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,strcmp(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abc</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abd</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">strcmp(\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abc\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abcd\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,strcmp(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abc</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abcd</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">strcmp(\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abc\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abbd\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,strcmp(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abc</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abbd</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));</span></div>
<br>
<img src ="http://www.cppblog.com/liujiajia/aggbug/51653.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-30 23:48 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/30/51653.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>strcat()函数</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/27/51323.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Tue, 27 May 2008 12:12:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/27/51323.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51323.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/27/51323.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51323.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51323.html</trackback:ping><description><![CDATA[strcat()接受两个字符串参数。将第二个字符串的一份拷贝添加到第一个字符串的结尾，从而使第一个字符串成为一个新的组合字符串，第二个字符串不改变。<br><br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;string1[</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">];<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;string2[]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">goodbye</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;gets(string1);<br>&nbsp;&nbsp;&nbsp;&nbsp;strcat(string1,string2);<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(string2);<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(string1);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}</span></div>
以上程序将string2字符串的拷贝放到了string1的结尾。<br><br>值得注意的是，string1在被使用strcat()函数之前，应该是已经初始化或者已经存储了一个字符串。因为如果字符串没有被初始化，string1[]里面元素的存储是一串随机值。strcat()需要找到string1的结尾（第一个出现的'\0'）,然后去掉这个'0',最后把string2[]的拷贝放到string1[]最后一个值的后面。
<img src ="http://www.cppblog.com/liujiajia/aggbug/51323.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-27 20:12 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/27/51323.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用strlen()获得字符串长度</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/27/51321.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Tue, 27 May 2008 11:32:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/27/51321.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51321.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/27/51321.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51321.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51321.html</trackback:ping><description><![CDATA[函数原型：size_t strlen(const char*)<br>作用：返回参数中字符串的长度。<br><br>以下代码是一个截取字符串的例子：<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;fit(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;msg[]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;moto</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(msg);<br>&nbsp;&nbsp;&nbsp;&nbsp;fit(msg,</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(msg);<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(msg</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;fit(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;size)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(strlen(</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">size)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">size)</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">\0</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></span></div>
截取前：<br>
<table style="WIDTH: 320px; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=3 border=1>
    <tbody>
        <tr>
            <td>H</td>
            <td>e</td>
            <td>l</td>
            <td>l</td>
            <td>o</td>
            <td>space</td>
            <td>m</td>
            <td>o</td>
            <td>t</td>
            <td>o</td>
            <td>\0</td>
        </tr>
    </tbody>
</table>
截取后<br>
<table style="WIDTH: 320px; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=3 border=1>
    <tbody>
        <tr>
            <td>H</td>
            <td>e</td>
            <td>l</td>
            <td>l</td>
            <td>o</td>
            <td>\0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
            <td>m</td>
            <td>o</td>
            <td>t</td>
            <td>o</td>
            <td>\0</td>
        </tr>
    </tbody>
</table>
<br>fit()是截取字符串的方法。如果字符串的长度大于要截取的长度，那么将指向该字符串的指针向后移动size位后，将指针指向的内容置为'\0'，即字符串结束。但string+size+1处仍然存在一个字符串。
<img src ="http://www.cppblog.com/liujiajia/aggbug/51321.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-27 19:32 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/27/51321.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>初步认识数组、指针和字符串关系</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/24/50913.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Fri, 23 May 2008 16:27:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/24/50913.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/50913.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/24/50913.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/50913.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/50913.html</trackback:ping><description><![CDATA[<p><strong><span style="FONT-SIZE: 18pt">字符串数组初始化：</span><br>(1)char arr[10]="HI";</strong><br>初始化字符串数组时，编译器自动将字符串最后一个字符后面加上'\0',以表示字符串的结束。<br>如果数组的大小大于字符串的长度+1，那么把字符串结束后面的元素也都初始化为'\0'; <br>看这段程序：<br>定义一个大小为12的字符串数组<br>char str1[12]="jiajia";&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>让一个char型的指针指向这个数组<br>&nbsp;&nbsp;&nbsp; char *p1=str1;<br>输出这个字符串<br>&nbsp;&nbsp;&nbsp; printf("%s\n\n",str1);<br>利用指针输出这个字符串数组中每个元素的字符、字符ascii值、字符存储地址<br>&nbsp;&nbsp;&nbsp; for(int i=0;i&lt;12;i++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%c:%d:%p\n",*p1,*p1,p1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p1++;<br>&nbsp;&nbsp;&nbsp; }<br>可以看出，数组元素的后几位都被初始化成了'\0';使用printf打印字符串数组时，程序遇到第一个'\0'就结束对字符串的读取。<br><br>如果数组的大小小于字符串的长度+1，程序在运行时可能要出现问题，所以应当确保数组的大小要大于字符串长度的大小+1；<br><br>这段程序的字符串长度+1小于数组的大小：<br>char str2[12]="hello jiajia";<br>&nbsp;&nbsp;&nbsp; char *p2=str2;</p>
<p>&nbsp;&nbsp;&nbsp; printf("\n\n\n%s\n\n",str2);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; for(int i=0;i&lt;28;i++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%c:%d:%p\n",*p2,*p2,p2);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p2++;<br>&nbsp;&nbsp;&nbsp; }<br><br>使用printf打印字符串，程序需要找到'\0'才能结束读取，结果找遍整个数组没有找到，只有继续顺着地址寻找，一直找到某个地址上的assii是0的，字符串才读取完毕。所以本段程序在读取完"hello jiajia"之后，后面可能还会有几个随机的字符，就是因为字符串终止于'\0'。<br><br><strong>(2)char arr[]="Hello World!";</strong><br>这种初始化方法不用担心数组大小过小的情况，数组的大小由编译器决定。<br><strong>(3)char arr[]={'H','e','e','l','o',' ','W','o','r','l','d','\0'};</strong><br>同第二种方法结果相同，但显然要麻烦许多。另外，' '代表空格，ascii值是32，'\0'代表字符串结束，ascii值是0；<br><strong>(4)char *p="Hello World";</strong><br>使用数组的方式初始化。同数组不同之处在于,p是一个变量，可以做递增、递减运算，而arr是数组的首地址，是一个地址常量，不能做递增、递减运算。<br><br>char *name="liujiajia";<br>&nbsp;&nbsp;&nbsp; name[0]='L';<br>&nbsp;&nbsp;&nbsp; printf("\n%s\n",name);<br>name[0]='L';这句代码在最新的c99标准中会引发程序异常。需要注意。<br><br><br>char *string="c programe";<br>&nbsp;&nbsp;&nbsp; char *string2;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp; string2=string;&nbsp;&nbsp;&nbsp;&nbsp;//将指针string的值赋给string2<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp; printf("\nstring=%s,&amp;string=%p,string=%p\n",string,&amp;string,string);<br>&nbsp;&nbsp;&nbsp; printf("\nstring=%s,&amp;string=%p,string=%p\n",string2,&amp;string2,string2);<br><br>上面这段程序最后显示，指针string所指向的地址和指针string2所指向的地址相同，这就说明它们指向的是内存里面同一个字符串，也就是说字符串本身并没有复制，而是产生了一个指向同一个字符串的指针。这样程序的效率会更加的高。如果需要复制字符串可以使用 strcpy() 或 strncpy()。<br><br></p>
<img src ="http://www.cppblog.com/liujiajia/aggbug/50913.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-24 00:27 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/24/50913.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>函数和二维数组的新认识(参数指定数组第二维和第一维大小)</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/20/50581.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Tue, 20 May 2008 14:31:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/20/50581.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/50581.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/20/50581.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/50581.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/50581.html</trackback:ping><description><![CDATA[<span style="COLOR: #000000">
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;ROWS&nbsp;3</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;COLS&nbsp;4</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;sum_rows(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr[][COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;rows);<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;junk[ROWS][COLS]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{{</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">},{</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">},{</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">11</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">}};<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;sum_rows(junk,ROWS);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求出每行的数值总和。&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}&nbsp;<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;sum_rows(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr[][COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;rows)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;r,c,tot;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;r</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">rows;r</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;c</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">COLS;c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">arr[r][c];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">row%d,sum=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,r,tot);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}</span></div>
<br>我们都知道上面的代码可以顺利执行， sum_rows()的形参是 int arr[][COLS],没有指定数组的第一维大小。但是设想一下，如果不指定第二维的大小，结果会怎样呢？<br>我写了以下例子：<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum2d(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr[][],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;row,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;col);&nbsp;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)&nbsp;<br>{&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr[3</span><span style="COLOR: #000000">][4</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{{</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">2,3,5</span><span style="COLOR: #000000">},{</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">4,4,5</span><span style="COLOR: #000000">},{2,3,4,5}};&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;sum2d(arr,3</span><span style="COLOR: #000000">,4</span><span style="COLOR: #000000">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;&nbsp;<br>}&nbsp;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum2d(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr[][],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;row,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;col)&nbsp;<br>{&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">row;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;j&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">col;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,arr[i][j]);&nbsp;&nbsp;invalid&nbsp;use&nbsp;of&nbsp;array&nbsp;with&nbsp;unspecified&nbsp;bounds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;<br>}&nbsp;<br></span></div>
</span>运行后发现，程序报错了，这是为什么呢？百思不得其解，后来请教了CSDN上老大们。终于稍微弄明白了点儿。我想具体应该从两方面证明为什么上面的程序不可行。<br>（1）从二维数组的存储方式上：二维数组在内存上是一行一行连续的存储的。如下图：<br>arr[3][4]<br>
<table style="WIDTH: 590px; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=3 border=1>
    <tbody>
        <tr>
            <td>arr[0][0]</td>
            <td>arr[0][1]</td>
            <td>arr[0][2]</td>
            <td>arr[0][3]</td>
            <td>arr[1][0]</td>
            <td>arr[1][1]</td>
            <td>arr[1][2]</td>
            <td>arr[1][3]</td>
            <td style="WIDTH: 28px; HEIGHT: 63px">...</td>
        </tr>
    </tbody>
</table>
<br>因为arr是这个数组的首地址，我们可以知道arr[0][0]的地址。只要我们知道arr[][4]中数组第二维的大小&#8216;4&#8217;，我们就可以找到任意行和列的地址。address=arr+(rows-1)*4+cols。但是如果不知道第二维的大小，那么条件就不够了。打一个比方，将若干个学生分成每组11人，并让他们站成一排，这时我们可以定位出第几组的第几个学生。但如果不知道每组11个人，那么我们显然就无法定位了。<br><br>（2）从指针的角度上看(这种思路我还是有点迷糊):大家可以参看这个帖子：<a href="http://topic.csdn.net/u/20080520/09/2122e210-5cf5-4b75-b31f-c2523b00cf53.html?575319732">http://topic.csdn.net/u/20080520/09/2122e210-5cf5-4b75-b31f-c2523b00cf53.html?575319732</a><br><br>恩，现在是越学越迷惑了。呵呵。<br>
<img src ="http://www.cppblog.com/liujiajia/aggbug/50581.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-20 22:31 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/20/50581.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>函数和二维数组</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/19/50446.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Mon, 19 May 2008 15:03:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/19/50446.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/50446.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/19/50446.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/50446.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/50446.html</trackback:ping><description><![CDATA[阅读下面代码，程序列举了3种二维数组作为参数传递的方法：<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;ROWS&nbsp;3</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;COLS&nbsp;4</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;sum_rows(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr[][COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;rows);<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;sum_cols(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;[][COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">可以省略名称</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum2d(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">arr)[COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;rows);&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">另一种语法形式</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;junk[ROWS][COLS]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{{</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">},{</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">},{</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">11</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">}};<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;sum_rows(junk,ROWS);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求出每行的数值总和。&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求出每列的数值总和。&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;sum_cols(junk,ROWS);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求出全部数值的总和。&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\nthe&nbsp;sum&nbsp;of&nbsp;the&nbsp;junk&nbsp;is&nbsp;%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,sum2d(junk,ROWS));<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}&nbsp;<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;sum_rows(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr[][COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;rows)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;r,c,tot;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;r</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">rows;r</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;c</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">COLS;c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">arr[r][c];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">row%d,sum=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,r,tot);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;sum_cols(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr[][COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;rows)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;r,c,tot;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;c</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">COLS;c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;r</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">rows;r</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">arr[r][c];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">col%d,sum=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,c,tot);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum2d(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">arr)[COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;rows)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;r,c,tot</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;r</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">rows;r</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;c</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">COLS;c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">arr[r][c];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;tot;<br>}<br></span></div>
<img src ="http://www.cppblog.com/liujiajia/aggbug/50446.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-19 23:03 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/19/50446.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>指针和多维数组</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/19/50440.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Mon, 19 May 2008 14:28:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/19/50440.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/50440.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/19/50440.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/50440.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/50440.html</trackback:ping><description><![CDATA[<strong>一、多维数组地址和指针的关系</strong><br>int arr2[4][2]={{1,2},{3,4},{5,6},{7,8}};<br>printf("arr2=%p,&amp;arr2[0]=%p,arr2[0]=%p,&amp;ar2r[0][0]=%p\n",arr2,&amp;arr2[0],arr2[0],&amp;arr2[0][0]);<br><br>
<table style="WIDTH: 364px; BORDER-COLLAPSE: collapse; HEIGHT: 91px" cellSpacing=0 cellPadding=3 border=1>
    <tbody>
        <tr>
            <td style="BACKGROUND-COLOR: #ccffcc">arr[0][0] (1)</td>
            <td>arr[1][0] (3)</td>
            <td>arr[2][0] (5)</td>
            <td>arr[3][0] (7)</td>
        </tr>
        <tr>
            <td style="BACKGROUND-COLOR: #ccffcc">arr[0][1] (2)</td>
            <td>arr[1][1] (4)</td>
            <td>arr[2][1] (6)</td>
            <td>arr[3][1] (8)</td>
        </tr>
    </tbody>
</table>
<br><br>上面的程序的结果输出竟然都一样。为什么呢？<br>首先 arr2 代表的是arr2数组的首地址，所以 arr2的值和 arr2第一个子元素arr2[0]的地址相同，即&nbsp;&amp;arr2[0]；<br>arr2[0]是含有两个整形元素一维数组的首地址，即&amp;arr2[0][0]的值。<br>简单的说：arr[0]是一个整数大小对象的地址，arr2是两个整数大小对象的地址。因为整数和两个整数组成的数组开始于同一个地址，所以 arr2 和 arr2[0] 具有相同的数值。<br><br><strong>二、指针（地址）运算和多维数组的关系</strong><br>printf("arr2+1=%p,arr2[0]+1=%p\n",arr2+1,arr2[0]+1);<br>现在我们知道，arr2 和 arr2[0]具有相同的数值，但是arr2+1和arr2[0]+1为什么数值不一样呢？这是因为，arr2指向的对象大小是两个int，儿arr2[0]所指向的大小是一个int。所以arr2+1的值要比arr2[0]+1的值多一个int大小。<br><br><strong>三、使用*对多维数组取值</strong><br>因为 arr2[0] 是 arr2[0][0]的首地址，所以 *(arr2[0])代表存储在arr2[0][0]的值。同样，arr2是arr2[0]的首地址，*arr就是 arr2[0]的值。但是 arr2[0]也是一个地址，即&amp;arr2[0][0],因此*arr2是 &amp;arr2[0][0]。所以 *&amp;arr2[0][0] 等价于 **arr2。*&amp;arr2[0][0] 可以简化为 arr2[0][0]。所以 arr2[0][0]==**arr2。<br>简而言之：arr2是地址的地址，所以需要经过两次取值才能取到具体的数值。<br><br><br>学完这个知识点，才发现在指针确实很难懂。<br>
<img src ="http://www.cppblog.com/liujiajia/aggbug/50440.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-19 22:28 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/19/50440.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>指针的基本运算</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/19/50421.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Mon, 19 May 2008 11:08:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/19/50421.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/50421.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/19/50421.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/50421.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/50421.html</trackback:ping><description><![CDATA[<p>对指针可以进行<strong>赋值</strong>、<strong>求值</strong>或<strong>取值</strong>、<strong>取指针的地址</strong>、<strong>将一个指针加上一个整数</strong>、<strong>增加指针的值</strong>、<strong>求差值</strong>的操作。请参看以下代码展示了几种情形：<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;urn[</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">200</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">300</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">400</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">500</span><span style="COLOR: #000000">};<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p1,</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p2,</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p3;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;p1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">urn;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">把一个地址赋给指针&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;p2</span><span style="COLOR: #000000">=&amp;</span><span style="COLOR: #000000">urn[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">取得urn[2]的地址，并赋给一个指针</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">数组各个元素的值和地址：\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">urn[%d]=%d&nbsp;&nbsp;&nbsp;&amp;urn[%d]=%p;\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,i,urn[i],i,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">urn[i]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n指针指向的地址,指针指向地址的值,指针的地址\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">p1=%p&nbsp;&nbsp;&nbsp;&nbsp;,*p1=%d&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&amp;p1=%p</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,p1,</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p1,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">p1);&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n指针加上整数的效果(指针+int!=指针，指针+int==地址)：\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">p1+4=%p,*(p1+4)=%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,p1</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(p1</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">printf("&amp;(p1+4)=%p",&amp;(p1+4))</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">error:p1+4是一个地址，不能对其进行取地址操作，但可以将p1+4赋给一个指针&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;p1</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">p++==p1+1。p1+1是一个地址，将p1+1赋给p1</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n在p1++以后：\np1=%p&nbsp;&nbsp;&nbsp;&nbsp;,*p1=%d&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,&amp;p1=%p\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,p1,</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p1,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">p1);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求两个指针差值&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\np2=%p,p1=%p,p2-p1=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,p2,p1,p2</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">p1);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}</span></div>
<img src ="http://www.cppblog.com/liujiajia/aggbug/50421.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-19 19:08 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/19/50421.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>带有数组类型参数的函数</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/16/50110.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Fri, 16 May 2008 15:54:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/16/50110.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/50110.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/16/50110.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/50110.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/50110.html</trackback:ping><description><![CDATA[<p>传递数组参数，其核心就是传递该数组的首地址。然后函数再通过数组、指针等，用该数组的首地址构造一个新的数组或指针，再通过传递过来的数组大小，对该数组进行操作。</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;SIZE&nbsp;4</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sumbyarr(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a[],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n);<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sumbypointer(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n);<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sumbyaddress(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;address,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n);<br>int sumbypointer2(int *begin,int *end)<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr[SIZE]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">30</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">40</span><span style="COLOR: #000000">};<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum1;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">使用数组作为形参接收数组&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;sum1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sumbyarr(arr,SIZE);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">the&nbsp;total&nbsp;of&nbsp;the&nbsp;arr&nbsp;by&nbsp;array&nbsp;is:%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,sum1);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">使用指针作为形参接收数组&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum2;<br>&nbsp;&nbsp;&nbsp;&nbsp;sum2</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sumbypointer(arr,SIZE);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">the&nbsp;total&nbsp;of&nbsp;the&nbsp;arr&nbsp;by&nbsp;pointer&nbsp;is:%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,sum2);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">使用地址作为形参接收数组&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum3;<br>&nbsp;&nbsp;&nbsp;&nbsp;sum3</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sumbyaddress(arr,SIZE);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">the&nbsp;total&nbsp;of&nbsp;the&nbsp;arr&nbsp;by&nbsp;address&nbsp;is:%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,sum3);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp; //使用两个指针形参接收<br>&nbsp;&nbsp;&nbsp; int sum4;<br>&nbsp;&nbsp;&nbsp; sum4=sumbypointer2(arr,arr+SIZE);<br>&nbsp;&nbsp;&nbsp; printf(<span style="COLOR: #000000">"</span><span style="COLOR: #000000">the&nbsp;total&nbsp;of&nbsp;the&nbsp;arr&nbsp;by&nbsp;pointer2 is:%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,sum4);<br></span><br><br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}&nbsp;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sumbyarr(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a[],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)<br>{&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;index;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;total</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(index</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;index</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">a[index];<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;total;<br>}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sumbypointer(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;index;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;total</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(index</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;index</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">,p</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total</span><span style="COLOR: #000000">+=*</span><span style="COLOR: #000000">p;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;total;<br>}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sumbyaddress(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;address,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">address;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;index;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;total</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(index</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;index</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n;index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">,p</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total</span><span style="COLOR: #000000">+=*</span><span style="COLOR: #000000">p;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;total;<br>}<br>int sumbypointer2(int *begin,int *end)<br>{<br>&nbsp;&nbsp;&nbsp; int total=0;<br>&nbsp;&nbsp;&nbsp; while(begin&lt;end)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; total+=*begin;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin++;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; return total;<br>}</span></div>
<br>以上代码演示怎样定义有数组作为参数的函数。<br>如果数组作为参数传递进入某个函数，并且在这个函数中改变了数组元素的值，那么程序返回后，这个数组元素的值有没有改变呢？<br><br>当然，值改变了，因为数组是通过地址传递的，改变了被调函数中数组的值也就意味着同时改变了主调函数中数组的值。因为它们的地址是相同的。 
<img src ="http://www.cppblog.com/liujiajia/aggbug/50110.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-16 23:54 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/16/50110.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>认识数组和指针的关系</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/16/50097.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Fri, 16 May 2008 13:30:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/16/50097.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/50097.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/16/50097.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/50097.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/50097.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp; &nbsp;int arr[SIZE]={1,2,3,4};<br>&nbsp;&nbsp;&nbsp; int index;<br>&nbsp;&nbsp;&nbsp; for(index=0;index&lt;SIZE;index++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("arr[%d]=%d address=%p\n",index,arr[index],arr+index);<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; int *p=arr;<br>&nbsp;&nbsp;&nbsp; for(index=0;index&lt;SIZE;index++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("*(p+%d)=%d&nbsp;&nbsp;&nbsp;&nbsp; p+%d=%p\n",index,*(p+index),index,p+index);<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; getchar();<br>&nbsp;&nbsp;&nbsp; return 0;<br><br>以上代码展示了读取数组中元素的两种方式，第一种是常规的使用数组下标来读取。第二种方法是使用指针来读取数组。<br><br>通过第一中方法中打印出来的内容，我们可以认识到，数组名其实就是这个数组的首地址，即：arr=数组首地址;arr+1:数组第二个元素的地址；arr+2：数组第三个元素的地址，以此类推。。。<br><br>第二种方法我们使用指针访问代替了数组下标访问，我们将 arr(即数组的首地址)赋值给了指针 *p，所以 p就等于arr的首地址，p+1就等于arr的二个元素的地址，那么*p就相当于了arr[0],*(p+1)就相当于arr[1]。<br><br>我们从中可以得出一个结论：数组标记实际上是一种变相使用指针的形式。
<img src ="http://www.cppblog.com/liujiajia/aggbug/50097.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-16 21:30 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/16/50097.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>二维数组简单使用</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/16/49998.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Thu, 15 May 2008 16:06:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/16/49998.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/49998.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/16/49998.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/49998.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/49998.html</trackback:ping><description><![CDATA[<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;PLAYER&nbsp;4</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;MATCH&nbsp;3</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">将4名球员近三场比赛的得分初始化</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arrmark[MATCH][PLAYER]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">},<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">},<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">输出具体成绩</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">***********Roony&nbsp;&nbsp;Ronaldo&nbsp;&nbsp;Kaka&nbsp;&nbsp;Grosso\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">MATCH;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">match%d:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,i);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">PLAYER;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%8d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,arrmark[i][j]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">打印总分</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;temp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">total&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">PLAYER;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求每个球员的总分&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">MATCH;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">arrmark[j][i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,temp);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>}&nbsp;</span></div>
<img src ="http://www.cppblog.com/liujiajia/aggbug/49998.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-16 00:06 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/16/49998.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>指定数组大小</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/15/49996.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Thu, 15 May 2008 15:32:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/15/49996.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/49996.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/15/49996.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/49996.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/49996.html</trackback:ping><description><![CDATA[指定数组大小也有很多方式：<br>&nbsp;&nbsp;&nbsp; char arr[10];&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;//<span style="COLOR: #008000">OK</span><br>&nbsp;&nbsp;&nbsp; char arr2[1+2*7];&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; //<span style="COLOR: #008000">OK<br></span>&nbsp;&nbsp;&nbsp; char arr3[sizeof(char)+2];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<span style="COLOR: #008000">OK</span><br>&nbsp;&nbsp;&nbsp; char arr4[-2];&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; //<span style="COLOR: #ff0000">error 数组大小不能使用负数</span><br>&nbsp;&nbsp;&nbsp; char arr5[0];&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; //<span style="COLOR: #008000">根据编译器不同，有的编译器转换为&#8220;arr5[]&#8221;,有的编译不通过。</span><br>&nbsp;&nbsp;&nbsp; char arr6[3.14];&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; //<span style="COLOR: #ff0000">error<br></span>&nbsp;&nbsp;&nbsp; char arr7[(int)3.14];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //<span style="COLOR: #008000">强制转换为int<br></span>&nbsp;&nbsp;&nbsp; int m=2;<br>&nbsp;&nbsp;&nbsp; char arr9[m];&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; //<span style="COLOR: #008000"><strong>c99前不允许</strong></span> <br><br>注意：c99标准允许使用变量指定数组大小。 
<img src ="http://www.cppblog.com/liujiajia/aggbug/49996.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-15 23:32 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/15/49996.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>认识数组之数组的初始化</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/15/49994.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Thu, 15 May 2008 15:11:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/15/49994.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/49994.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/15/49994.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/49994.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/49994.html</trackback:ping><description><![CDATA[以下代码展示数组初始化的各种情况：<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;SIZE&nbsp;4</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">一般初始化&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr[SIZE]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">};<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;index;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(index</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;index</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">SIZE;index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[index]</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">arr[%d]=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,index,arr[index]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">定义并初始化一个const数组。数组内元素不能修改&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;arr2[SIZE]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">};<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">数组不初始化，元素的值为随机数。&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr3[SIZE];&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(index</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;index</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">SIZE;index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">arr3[%d]=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,index,arr3[index]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">如果初始化数组中部分元素，其他元素则被初始化为0&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr4[SIZE]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">};&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(index</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;index</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">SIZE;index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">arr4[%d]=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,index,arr4[index]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">指定要初始化的元素</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr5[SIZE]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,[</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">};<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(index</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;index</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">SIZE;index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">arr5[%d]=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,index,arr5[index]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">数组的元素个数不固定&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr6[]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">};<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">sizeof&nbsp;arr6=%d,sizeof&nbsp;arr6[0]=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">&nbsp;arr6,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">&nbsp;arr6[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(index</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;index</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">&nbsp;arr6</span><span style="COLOR: #000000">/</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">&nbsp;arr6[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">arr6[%d]=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,index,arr6[index]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}</span></div>
<br>程序运行结果是：<br><img height=426 alt="" src="http://www.cppblog.com/images/cppblog_com/liujiajia/arrinit.jpg" width=284 border=0><br>怎么样？数组的初始化方式多种多样。但要灵活运用。
<img src ="http://www.cppblog.com/liujiajia/aggbug/49994.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-15 23:11 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/15/49994.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>认识sizeof</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/15/49942.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Thu, 15 May 2008 08:01:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/15/49942.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/49942.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/15/49942.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/49942.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/49942.html</trackback:ping><description><![CDATA[sizeof 是C语言里面的一个运算符，并且以字节为单位返回其操作数的大小。操作数可以使一个具体的数据对象、变量类型、一个操作数。并且返回类型为size_t，size_t是在stddef.h头文件里定义的，原型是：typedef&nbsp;unsigned&nbsp;int&nbsp;size_t;<br>以下代码演示sizeof用法：<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stddef.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;<br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdlib.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">sizeof(2008)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">2008</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">sizeof(i)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(i));<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">sizeof&nbsp;i=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">&nbsp;i);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">sizeof(int)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">printf("sizeof&nbsp;int=%d\n",sizeof&nbsp;int);&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">sizeof&nbsp;int写法错误！&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">sizeof(char)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">sizeof(float)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">float</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">sizeof(double)=%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">));<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}</span></div>
<br>运行结果显示：<br><img height=134 alt="" src="http://www.cppblog.com/images/cppblog_com/liujiajia/sizeof.jpg" width=264 border=0><br><br>另外，sizeof 可以应用于数组、结构等情况，请参考这篇文章：<a href="http://blog.chinaunix.net/u/20828/showart_438003.html">http://blog.chinaunix.net/u/20828/showart_438003.html</a>
<img src ="http://www.cppblog.com/liujiajia/aggbug/49942.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-15 16:01 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/15/49942.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>认识递归函数</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/14/49769.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Tue, 13 May 2008 16:08:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/14/49769.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/49769.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/14/49769.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/49769.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/49769.html</trackback:ping><description><![CDATA[<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;testfunc(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;testfunc(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;testfunc(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Lv%d:%p\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,n,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">%p&nbsp;地址显示格式</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(n</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testfunc(n</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">testfunc()调用了自己</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">2Lv%d:%p\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,n,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);<br>}<br></span></div>
以上是一个递归的示例。<br>testfunc()在内部调用了自己，这样的后果是：暂时不执行最后面的&#8220;printf("2Lv%d:%p\n",n,&amp;n);&#8221;，而是将 n+1作为参数调用了自己，这样重复，最终程序的结果是：<br><img height=158 alt="" src="http://www.cppblog.com/images/cppblog_com/liujiajia/c.jpg" width=312 border=0><br>(根据电脑不同，地址可能有所不同)<br>程序具体运行步骤：<br>1.main()调用testfunc(1)<br>2.testfunc(1)执行，打印&#8220;lv1:0022FF60&#8221;，0022FF60是n的地址<br>3.n&lt;4,条件成立，&nbsp;testfunc(1)调用testfunc(2)<br>4.testfunc(2)执行，打印&#8220;lv1:0022FF40&#8221;,因为此时testfunc(2)的变量n已经不是testfunc(1)中的变量n了，所以地址不同。（函数的参数作用域只在当前函数）<br>5.n&lt;4，条件成立，testfunc(2)调用testfunc(3)<br>6.testfunc(3)执行，打印&#8220;lv1:0022FF20&#8221;<br>7.n&lt;4，条件成立，testfunc(3)调用testfunc(4)<br>8.testfunc(4)执行，打印&#8220;lv1:0022FF00&#8221;<br>9.n&lt;4,条件不成立，打印&#8220;2lv1:0022FF00&#8221;<br>10.返回调用testfunc(4)的testfunc(3),打印&#8220;2lv1:0022FF20&#8221;<br>11.返回调用testfunc(3)的testfunc(2),打印&#8220;2lv1:0022FF40&#8221;<br>12.返回调用testfunc(2)的testfunc(1),打印&#8220;2lv1:0022FF60&#8221;<br><br>OK,这就是函数的递归调用，通俗的说就是函数自己调用自己。以前学时没有弄明白，现在终于弄明白了。<br>
<img src ="http://www.cppblog.com/liujiajia/aggbug/49769.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-14 00:08 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/14/49769.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>认识getchar()和putchar()</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/09/49367.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Fri, 09 May 2008 14:55:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/09/49367.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/49367.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/09/49367.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/49367.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/49367.html</trackback:ping><description><![CDATA[getchar()函数没有参数，返回输入设备的下一个字符。<br>ch=getchar(); 和 scanf("%c",&amp;ch);是同样的效果。<br><br><br>putchar()函数用来打印它的参数。<br>putchar(ch); 和 printf("%c",ch); 是同样的效果。<br><br>这两个函数都在 stdio.h 文件中定义了。 <br><br>
<img src ="http://www.cppblog.com/liujiajia/aggbug/49367.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liujiajia/" target="_blank">刘加加</a> 2008-05-09 22:55 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/09/49367.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>