﻿<?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++语言学习中)</title><link>http://www.cppblog.com/liujiajia/</link><description>学习、工作、生活，这是三个问题。</description><language>zh-cn</language><lastBuildDate>Sun, 05 Apr 2026 13:56:36 GMT</lastBuildDate><pubDate>Sun, 05 Apr 2026 13:56:36 GMT</pubDate><ttl>60</ttl><item><title>最简单的windows程序</title><link>http://www.cppblog.com/liujiajia/archive/2008/07/23/56963.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Wed, 23 Jul 2008 09:01:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/07/23/56963.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/56963.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/07/23/56963.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/56963.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/56963.html</trackback:ping><description><![CDATA[<p>1.打开vc++;<br>2.选择菜单栏file----new；<br>3.选项卡选择Projects；<br>4.模板选择 Win32 Application;<br>5.输入Project name；<br>6.Loction 选择路径；<br>7.点击OK继续；<br>8.选择默认的&#8220;An Empty project&#8221;；<br>9.点击finish完成创建工程；<br>10.向工程里面添加一个c++文件；（file--new--选项卡file--C++ source file--输入文件名--点击OK）<br>11.输入以下代码；</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: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;WIN32_LEAN_AND_MEAN</span><span style="COLOR: #000000"><br><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">windows.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">windowsx.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;WINAPI&nbsp;WinMain(HINSTANCE&nbsp;hinstance,HINSTANCE&nbsp;hprevinstance,LPSTR&nbsp;lpcmdline,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ncmdshow)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;MessageBox(NULL,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World!</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">first&nbsp;win</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,MB_OK</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">MB_ICONEXCLAMATION);<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>
<p>12.保存，按ctrl+F5编译并运行。<br>13.弹出一个对话框。<br><img height=113 alt="" src="http://www.cppblog.com/images/cppblog_com/liujiajia/alert.gif" width=144 border=0><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;以上就是号称世界上最简单的Windows程序！<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<img src ="http://www.cppblog.com/liujiajia/aggbug/56963.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-07-23 17:01 <a href="http://www.cppblog.com/liujiajia/archive/2008/07/23/56963.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>函数返回值的特殊情况（返回引用、非引用、引用左值）</title><link>http://www.cppblog.com/liujiajia/archive/2008/07/16/56244.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Tue, 15 Jul 2008 16:41:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/07/16/56244.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/56244.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/07/16/56244.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/56244.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/56244.html</trackback:ping><description><![CDATA[<strong><span style="FONT-SIZE: 14pt">返回非引用</span><br></strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string make_plural(size_t i,const string &amp;word,const string &amp;ending)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (i==1)?word:word+ending;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;以上函数，当i等于1时，函数返回word形参的副本；当i不等于1时函数返回一个临时的string对象，这个临时对象是由字符串word和ending相加而成的。这两种情况下，return都在调用该函数的地方防复制了返回的string对象。<br><br><span style="FONT-SIZE: 14pt"><strong>返回引用</strong></span><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const string &amp;shorterString(const string &amp;s1,const string &amp;s2)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return s1.size()&lt;s2.size()?s1:s2;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;以上函数的返回值是引用类型。无论返回s1或是s2,调用函数和返回结果时，都没有复制这些string对象。<br><br><strong style="COLOR: red">千万不要返回局部对象的引用</strong><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const string &amp;mainip(const string &amp;s)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string ret=s;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ret;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当函数执行完毕，程序将释放分配给局部对象的存储空间。此时，对局部对象的引用就会指向不确定的内存。（我在dev c++里运行通过。。。。 但是vc6和vc2008不能编译通过）。同理，也不能返回局部对象的指针。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br><span style="FONT-SIZE: 14pt"><strong>引用返回左值（比较奇妙）</strong></span><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char &amp;get_val(string &amp;str,string::size_type ix)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return str[ix];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;使用语句调用:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string s("123456");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get_val(s,0)='a';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;把函数应用于左值，还是第一次见到（真是孤陋寡闻）。<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: #008000">//</span><span style="COLOR: #008000">liujiajia<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">2008-7-16</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;make_plural(size_t,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">shorterString(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">mainip(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">get_val(</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">::size_type);<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;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">make_plural(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">dog</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">make_plural(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">dog</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;string1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">1234</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;string2</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abc</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">shorterString(string1,string2)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">mainip(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">jiajia</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;s(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">123456</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;get_val(s,</span><span style="COLOR: #000000">0</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;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<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>}<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">返回非引用&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;make_plural(size_t&nbsp;i,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">word,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">ending)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">word:word</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">ending;<br>}<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">返回引用&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">shorterString(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">s1,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">s2)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;s1.size()</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">s2.size()</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">s1:s2;<br>}<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">禁止返回局部对象的引用（我的dev&nbsp;c++&nbsp;没有报错，比较可怕）&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">mainip(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">s)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;ret</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">s;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;ret;<br>}<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">引用返回左值（第一次听说）&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">get_val(</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">str,</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">::size_type&nbsp;ix)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;str[ix];<br>}</span></div>
<br>
<img src ="http://www.cppblog.com/liujiajia/aggbug/56244.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-07-16 00:41 <a href="http://www.cppblog.com/liujiajia/archive/2008/07/16/56244.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数组的形参</title><link>http://www.cppblog.com/liujiajia/archive/2008/07/15/56143.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Mon, 14 Jul 2008 16:05:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/07/15/56143.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/56143.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/07/15/56143.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/56143.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/56143.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数组有两个特殊的性质，影响我们定义和使用作用在数组上的函数：一是不能复制数组。二是使用数组名字时，数组会自动转换为指向其第一个元素的指针。因为数组不能复制，所以无法编写使用数组类型形参的函数。因为数组会被初始化为指针，所以处理数组的函数通常通过操作指向数组中的元素的指针来处理数组。<br><br>示例：<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//形式1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void printValues(const int ia[10])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//虽然形参指定了数组的大小，但编译时不会检查实参数组大小，因为编译器只检查形参是否是指针，并且指针类型和数组元素的类型是否匹配。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(size_t i=0;i!=10;i++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;ia[i]&lt;&lt;endl;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//形式2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void printValues2(const int ia[],int size)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(size_t i=0;i!=size;i++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;ia[i]&lt;&lt;endl;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int main(void)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int i[10]={1,2,3,4,5,6,7,8,9,0};<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printValues(i);&nbsp;&nbsp;&nbsp;//i作为printValues的实参，在这里为数组i的首地址，或者说他转换为成一个指针<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printValues2(i,10);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数组形参可以定义为引用或非引用类型。大部分情况下，数组以普通的非引用类型传递,此时数组会悄悄的转换为指针。一般来说，非引用类型的形参会初始化为其相应实参的副本。但是在传递数组时，实参是指向数组第一个元素的指针，形参复制的是这个指针的值，而不是数组元素本身。函数操作的也是指针的副本，因此不会修改实参指针的值。然而，函数可通过该指针修改它所指向的数组元素的值。<br><br>以上代码可以使用指针表示，效果等同：<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void printValues3(const int *ia,int size)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(size_t i=0;i!=size;i++,ia++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;*ia&lt;&lt;endl;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br><br>示例2（通过引用传递数组）：<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void printValues4(int (&amp;arr)[10])&nbsp;&nbsp;&nbsp;&nbsp;//这种情况下，数组大小成为形参和实参的一部分。编译器会检查数组大小是否匹配。&nbsp;int (&amp;arr)[10] 中的小括号是必须的。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(size_t i=0;i!=10;i++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;arr[i]&lt;&lt;endl;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int main(void)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printValues4(i);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;和其他类型一样，数组形参可声明为数组的引用。如果形参是数组的引用，编译器不会将数组实参转化为指针，而是传递数组的引用本身。
<img src ="http://www.cppblog.com/liujiajia/aggbug/56143.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-07-15 00:05 <a href="http://www.cppblog.com/liujiajia/archive/2008/07/15/56143.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>传递指向指针的引用</title><link>http://www.cppblog.com/liujiajia/archive/2008/07/14/56135.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Mon, 14 Jul 2008 14:19:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/07/14/56135.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/56135.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/07/14/56135.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/56135.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/56135.html</trackback:ping><description><![CDATA[&nbsp;代码示例：<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: #008000">//</span><span style="COLOR: #008000">liujiajia<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">2008-7-14&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;ptrswap(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*&amp;</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*&amp;</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</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">int</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">20</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">pi</span><span style="COLOR: #000000">=&amp;</span><span style="COLOR: #000000">i;<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">pj</span><span style="COLOR: #000000">=&amp;</span><span style="COLOR: #000000">j;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">调用ptr()之前:pi=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">pi</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,*pi=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">pi</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;pj=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">pj</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,*pj=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">pj</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;ptrswap(pi,pj);<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">调用ptr()之后:pi=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">pi</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,*pi=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">pi</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;pj=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">pj</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,*pj=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">pj</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<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>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;ptrswap(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*&amp;</span><span style="COLOR: #000000">v1,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*&amp;</span><span style="COLOR: #000000">v2)<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">temp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">v1;<br>&nbsp;&nbsp;&nbsp;&nbsp;v1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">v2;<br>&nbsp;&nbsp;&nbsp;&nbsp;v2</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">temp;<br>}</span></div>
<br>在调用ptrswap之前，pi指向i,pj指向j。在调用ptrswap之后，pi指向j,而pj指向了i。
<img src ="http://www.cppblog.com/liujiajia/aggbug/56135.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-07-14 22:19 <a href="http://www.cppblog.com/liujiajia/archive/2008/07/14/56135.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用引用形参的优点和注意事项</title><link>http://www.cppblog.com/liujiajia/archive/2008/07/14/56131.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Mon, 14 Jul 2008 13:48:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/07/14/56131.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/56131.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/07/14/56131.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/56131.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/56131.html</trackback:ping><description><![CDATA[<p><span style="FONT-SIZE: 14pt"><strong>使用引用形参的优点：</strong></span><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在向函数传递大对象时，需要使用引用形参。虽然赋值实参对于内置数据类型的对象或者规模较小的类类型来说没有什么问题，但是对于大部分的类类型或者大型数组，它的效率就比较低了。另外，某些类类型是无法复制的。使用引用形参，函数可以直接访问实参对象，而无须复制它。<br><br>示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool isShorter(const string &amp;str1,const string &amp;str2)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return str1.size()&lt;str2.size();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;由于string对象可能非常的长，我们希望避免复制操作。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>如果使用引用形参的唯一目的是避免复制形参，则应将形参定义为const引用。<br></strong><br><span style="FONT-SIZE: 14pt"><strong>使用引用形参应该注意的：</strong></span><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int incr(int &amp;val)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ++val;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void main(void)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; short v1=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const int v2=10;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int v3=20;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;incr(v1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//错误，v1不是int<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;incr(v2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//错误，v2是const<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;incr(v3);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//正确<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;incr(10);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//错误&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;所以，调用非const类型的引用形参，实参必须不是const类型的，而且实参的类型和形参的类型应当一致。</p>
<img src ="http://www.cppblog.com/liujiajia/aggbug/56131.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-07-14 21:48 <a href="http://www.cppblog.com/liujiajia/archive/2008/07/14/56131.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用引用形参返回额外的信息</title><link>http://www.cppblog.com/liujiajia/archive/2008/07/14/56085.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Mon, 14 Jul 2008 04:12:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/07/14/56085.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/56085.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/07/14/56085.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/56085.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/56085.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;一个函数只能有一个返回值，但如果我想让他返回更多的信息怎么办呢？这种情况可以使用引用形参。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;因为引用形参在函数调用后是可以修改实参值的，所以就相当与函数的返回值。<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;下面的代码演示了一个带有一个返回值，并通过引用形参向主调函数返回信息的函数。这个函数的功能是找出在容器中某一个数值出现的次数，并返回该数值第一次出现的迭代器。<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: #008000">//</span><span style="COLOR: #008000">liujiajia<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">2008-7-14</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br>vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::const_iterator&nbsp;find_val(vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::const_iterator,vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::const_iterator,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">,vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::size_type&nbsp;</span><span style="COLOR: #000000">&amp;</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;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;some;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;val;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">使用push_back()输入容器中的值&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">val)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;some.push_back(val);<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;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::size_type&nbsp;ccurs;<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;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::const_iterator&nbsp;it</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">find_val(some.begin(),some.end(),</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">,ccurs);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">ccurs</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>}<br><br>vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::const_iterator&nbsp;find_val(<br>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::const_iterator&nbsp;beg,<br>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::const_iterator&nbsp;end,<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;value,<br>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::size_type&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">occurs)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::const_iterator&nbsp;res_iter</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">end;<br>&nbsp;&nbsp;&nbsp;&nbsp;occurs</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">(;beg</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">end;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">beg)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">beg</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">value)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">这个if语句的作用是让res_iter等于值第一次出现的位置&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(res_iter</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">end)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res_iter</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">beg;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;occurs</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<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;res_iter;<br>}</span></div>
<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<img src ="http://www.cppblog.com/liujiajia/aggbug/56085.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-07-14 12:12 <a href="http://www.cppblog.com/liujiajia/archive/2008/07/14/56085.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>函数的形参（非引用形参、指针形参、引用形参）</title><link>http://www.cppblog.com/liujiajia/archive/2008/07/14/56054.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Sun, 13 Jul 2008 16:30:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/07/14/56054.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/56054.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/07/14/56054.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/56054.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/56054.html</trackback:ping><description><![CDATA[一、非引用形参<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void add1(int v1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v1+=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这是最普通的形参方式，当函数被调用时，实参的副本初始化形参，函数并没有访问调用所传递的实参，因此v1+=1不会修改实参的值。对v1的操作只是修改了实参的一个副本。<br><br>二、指针形参<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void add2(int *p)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*p+=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p+=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;使用指针做为函数的形参，同样指针的值（指针的地址）不会因为p+=1而受到影响，但是指针指向的地址的值（*p）将会改变。所以要想修改实参的值，可以使用这种方法。但是还有一种更安全更自然的方法-引用形参<br><br>三、引用形参<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void swap(int &amp;a,int &amp;b)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int temp=a;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a=b;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b=temp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;引用形参直接关联到其所绑定的对象，而非这些对象的副本。 所以这种方法可以修改实参的值，而且更加直观。<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: #008000">//</span><span style="COLOR: #008000">liujiajia<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">2008-7-14&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;add1(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;add2(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;swap(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</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;n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">调用add1()之前n=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;add1(n);<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">调用add1()之后n=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;<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">=&amp;</span><span style="COLOR: #000000">n;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">调用add2()之前n=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,p=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;add2(p);<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">调用add2()之后n=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,p=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,b</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">调用swap()之前a=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">a</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,b=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">b</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;swap(a,b);<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">调用swap()之后a=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">a</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,b=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">b</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>}<br><br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">非引用形参&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;add1(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;v1)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;v1</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>}<br><br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">指针形参</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;add2(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>}&nbsp;<br><br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">引用形参</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;swap(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">a,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">b)<br>{<br>&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">a;<br>&nbsp;&nbsp;&nbsp;&nbsp;a</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">b;<br>&nbsp;&nbsp;&nbsp;&nbsp;b</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">temp;<br>}<br></span></div>
<img src ="http://www.cppblog.com/liujiajia/aggbug/56054.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-07-14 00:30 <a href="http://www.cppblog.com/liujiajia/archive/2008/07/14/56054.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>创建和使用动态数组</title><link>http://www.cppblog.com/liujiajia/archive/2008/07/12/55984.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Sat, 12 Jul 2008 15:26:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/07/12/55984.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/55984.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/07/12/55984.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/55984.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/55984.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;动态数组，顾名思义，就是可以动态分配长度的数组。在c语言中，数组的长度是固定的，而这种限制显然是很不灵活的。在c++中，数组有时候不用在编译时就知道长度，直到运行时才知道。与普通的数组变量不同，动态分配的数组将一直存在，直到程序显式的释放它为止。<br><br><strong style="FONT-SIZE: 14pt">定义：</strong><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int *p=new int[20];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p就是一个动态数组，这时我们发现，其实所谓的动态数组就是一个指针，使用new为p分配长度。我们可以通过这种方式初始化： int *p=new int[20]();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;同样上面的语句等同于：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int n=20;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int *p=new int[n];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;因为n是一个变量，所以数组的长度是动态的。<br><br><strong style="FONT-SIZE: 14pt">操作实例：</strong><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int n=20;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int *p=new int[n];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p可以使用数组下标为其赋值：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(int i=0;i&lt;n;i++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p[i]=i+3;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;上面这种方式没有问题，但我想既然p是一个指针，那么使用指针操作一样也可以吧？<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(int i=0;i&lt;n;i++,p++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*p=i;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这样就可以了，将他们输出：（这时指针p又指向了最后）<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p--;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(int *p2=p-10;p2!=p;p--)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;*p&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br><span style="FONT-SIZE: 14pt"><strong>动态空间释放：</strong></span><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;最后重要的是别忘了将动态创造的空间释放掉，语句是：delete [] p;&nbsp;&nbsp;&nbsp;&#8216;[ ]&#8217;表明该指针是指向的自由存储区的数组，而非单个对象。如果遗漏了空方括号，编译器将无法发现这个错误，将导致程序在运行时出错。<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">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<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;n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">20</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">p</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[n];<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">n;i</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;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;p</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">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p2</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">n;p2</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">p;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;cout</span><span style="COLOR: #000000">&lt;&lt;*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;[]&nbsp;p;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>}</span></div>
<br>
<img src ="http://www.cppblog.com/liujiajia/aggbug/55984.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-07-12 23:26 <a href="http://www.cppblog.com/liujiajia/archive/2008/07/12/55984.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>const和指针（指向const对象的指针、const指针）</title><link>http://www.cppblog.com/liujiajia/archive/2008/06/21/54166.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Fri, 20 Jun 2008 16:13:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/06/21/54166.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/54166.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/06/21/54166.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/54166.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/54166.html</trackback:ping><description><![CDATA[<span style="FONT-SIZE: 14pt"><strong>1.指向const对象的指针</strong></span><br><br>const int *p;<br>这个p是一个指向int类型const对象的指针，const限定了指针p所指向的类型，而并非p本身。也就是说p本身并不是const。在定义时不需要对它进行初始化，还可以给p重新赋值，使其指向另一个const对象。但不能通过p修改所指向对象的值。<br>示例1：int a=0; p=&amp;a;&nbsp; 可以。<br>示例2：*p=20;&nbsp; 不可以。<br>结论：这种指向const对象的指针只是限制不能修改p指向对象的数值，而不是限制p指向什么对象。<br><br>把一个const对象的地址赋给一个不是指向const对象的指针也是不行的。<br>示例3：const int b=10;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int *p2=&amp;b;&nbsp;&nbsp; //error<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const int *p3=&amp;b; //ok<br>结论：因为变量b有const修饰，不能被修改。但指针p2是一个普通的指针，可以修改指向对象的值，两种声明矛盾，所以不合法。而指向const对象的指针不允许修改指针指向对象的数值，所以这种方式合法。<br><br><span style="FONT-SIZE: 14pt"><strong>2.const指针</strong></span><br><br>int c=20;<br>int *const p4=&amp;c;<br><br>指针p4称为const指针。它和指向const对象的指针恰好相反，它不能够修改所指向对象，但却能够修改指向对象的数值。另外，这种指针在声明时必须初始化。<br><br><span style="FONT-SIZE: 14pt"><strong>3.指向const对象的const指针</strong></span><br><br>const int&nbsp;d=30;<br>const int *const dp=&amp;d;<br><br>指针dp既不能修改指向的对象，也不能修改只想对象的值。
<img src ="http://www.cppblog.com/liujiajia/aggbug/54166.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-06-21 00:13 <a href="http://www.cppblog.com/liujiajia/archive/2008/06/21/54166.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>标准库 bitset 类型</title><link>http://www.cppblog.com/liujiajia/archive/2008/06/06/52400.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Fri, 06 Jun 2008 15:04:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/06/06/52400.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/52400.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/06/06/52400.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/52400.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/52400.html</trackback:ping><description><![CDATA[有些程序要处理二进制有序集，每个位可能包含0值或者1值。位是保存一组项或条件yes/no信息的简洁方法。标准库提供的bitset类简化了位集的处理。使用bitset 类型，必须包含头文件：#include&lt;bitset&gt;<br><br>bitset对象的定义和初始化<br><br>bitset&lt;n&gt; b;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b有n位，每位都是0；<br>bitset&lt;n&gt; b(u);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b是unsigned long型u的副本；<br>bitset&lt;n&gt; b(s);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b是string对象s中含有的位串的副本；<br>bitset&lt;n&gt; b(s,pos,n);&nbsp;&nbsp;&nbsp;b是s中从位置pos开始的n个位的副本；<br><br>示例：<br>&nbsp;&nbsp;&nbsp;&nbsp;bitset&lt;16&gt; bitvec1(0xffff);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//bitvec1的0-15位都是1<br>&nbsp;&nbsp;&nbsp; bitset&lt;32&gt; bitvec2(0xffff);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//bitvec2的0-15为是1，16-31是0<br>&nbsp;&nbsp;&nbsp; bitset&lt;128&gt; bitvec3(0xffff);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//bitvec3的0-15位是1，16-127都是0<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp; string str("1100");<br>&nbsp;&nbsp;&nbsp; bitset&lt;32&gt; bitvec4(str);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//bitvec4的第2、第3位是1，其余都是0。因为string对象和bitset对象之间是反向转化的：string对象的最右边字符（即下标最大的那个字符）用来初始化bitset对象的低价位（即小标为0的位）<br><br>bitset对象的操作：<br><br>b.any()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b中是否存在置为1的二进制位？<br>b.none()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b中不存在置为1的二进制位吗？<br>b.count()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b中值为1的二进制位的个数<br>b.size()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b中二进制位的个数<br>b[pos]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 访问b中在pos处的二进制位<br>b.test(pos)&nbsp;&nbsp;&nbsp; b中在pos处的二进制位是否为1？<br>b.set()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;把b中所有的二进制位都置为1<br>b.set(pos)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;把b中在pos处的二进制位置为1<br>b.reset()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;把b中所有的二进制位都置为0<br>b.reset(pos)&nbsp;&nbsp;&nbsp;&nbsp;把b中在pos处的二进制位置为0<br>b.flip()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;把b中所有二进制位逐位取反<br>b.flip(pos)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;把b中在pos处的二进制位取反<br>b.to_ulong()&nbsp;&nbsp;&nbsp;用b中同样的二进制位返回一个unsigned long值<br>os&lt;&lt;b&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;吧b中的位集输出到os流<br>
<img src ="http://www.cppblog.com/liujiajia/aggbug/52400.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-06-06 23:04 <a href="http://www.cppblog.com/liujiajia/archive/2008/06/06/52400.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>迭代器 iterator</title><link>http://www.cppblog.com/liujiajia/archive/2008/06/06/52399.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Fri, 06 Jun 2008 14:32:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/06/06/52399.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/52399.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/06/06/52399.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/52399.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/52399.html</trackback:ping><description><![CDATA[<strong style="FONT-SIZE: 14pt">迭代器</strong>(iterator)是一中检查容器内元素并遍历元素的数据类型。<br><br>每种容器类型都定义了自己的迭代器类型，如vector:<br>vector&lt;int&gt;::iterator iter;这条语句定义了一个名为iter的变量，它的数据类型是由vector&lt;int&gt;定义的iterator类型。<br><br><strong>使用迭代器读取vector中的每一个元素：<br></strong>&nbsp;&nbsp;&nbsp;vector&lt;int&gt; ivec(10,1);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; for(vector&lt;int&gt;::iterator iter=ivec.begin();iter!=ivec.end();++iter)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *iter=2;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//使用 * 访问迭代器所指向的元素<br>&nbsp;&nbsp;&nbsp; }<br><br><br><strong>const_iterator:</strong><br>只能读取容器中的元素，而不能修改。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(vector&lt;int&gt;::const_iterator citer=ivec.begin();citer!=ivec.end();citer++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;*citer;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //*citer=3;&nbsp;&nbsp; error<br>&nbsp;&nbsp;&nbsp; }<br><br><strong>vector&lt;int&gt;::const_iterator 和 const vector&lt;int&gt;::iterator的区别<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;const vector&lt;int&gt;::iterator newiter=ivec.begin();<br>&nbsp;&nbsp;&nbsp; *newiter=11;&nbsp;&nbsp;&nbsp;//可以修改指向容器的元素<br>&nbsp;&nbsp;&nbsp; //newiter++;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//迭代器本身不能被修改 <br><br><strong>iterator的算术操作：<br></strong>iterator除了进行++,--操作，可以将iter+n,iter-n赋给一个新的iteraor对象。还可以使用一个iterator减去另外一个iterator.<br>&nbsp;&nbsp;&nbsp;&nbsp;const vector&lt;int&gt;::iterator newiter=ivec.begin();<br>&nbsp;&nbsp;&nbsp;&nbsp;vector&lt;int&gt;::iterator newiter2=ivec.end();<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"\n"&lt;&lt;<strong>newiter2-newiter</strong>;
<img src ="http://www.cppblog.com/liujiajia/aggbug/52399.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-06-06 22:32 <a href="http://www.cppblog.com/liujiajia/archive/2008/06/06/52399.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>认识标准库vector类型</title><link>http://www.cppblog.com/liujiajia/archive/2008/06/02/51928.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Mon, 02 Jun 2008 15:48:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/06/02/51928.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51928.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/06/02/51928.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51928.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51928.html</trackback:ping><description><![CDATA[vector是同一种类型的对象的集合，每个对象都有一个对应的整数索引值。和string对象一样，标准库将负责管理与存储元素相关的内存。我们把vector称之为容器。使用vector类型必须包含 include&lt;vector&gt;。<br>vector 是一个类模板(class template)。<br><br><strong style="FONT-SIZE: 14pt">vector的定义和初始化：</strong><br>&nbsp;&nbsp;&nbsp; vector&lt;string&gt; text;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//保存类型为string的对象，默认构造函数text为空<br>&nbsp;&nbsp;&nbsp; vector&lt;string&gt; text2(10);&nbsp;&nbsp;&nbsp;//保存类型为string的对象，包含10个空字符串<br>&nbsp;&nbsp;&nbsp; vector&lt;int&gt; ive(10,9);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//保存int的对象，保存10个值为9的整形变量<br>&nbsp;&nbsp;&nbsp; vector&lt;int&gt; ive2(ive);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//使用ive初始化ive2<br><br><span style="FONT-SIZE: 14pt"><strong>向vector添加元素：<br></strong></span>&nbsp;&nbsp;&nbsp;while(cin&gt;&gt;word)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; text.<strong>push_back</strong>(word);&nbsp;&nbsp;//使用push_back可以向vector的后面插入新的元素。<br>&nbsp;&nbsp;&nbsp; } <br>注意：*vector对象（以及其他标准库容器对象）的重要属性就在于可以在运行时高效的添加元素，因为vector增长效率高，在元素值已知的情况下，最好是动态的添加元素。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*虽然可以对给定元素个数的vector对象预先分配内存，但更有效的方法是初始化一个空vector对象，然后再动态的增加元素。<br><br><strong style="FONT-SIZE: 14pt">vector的下标操作：</strong><br>for(vector&lt;string&gt;::size_type ix=0;ix!=text.<strong>size</strong>();++ix)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;text<strong>[ix]</strong>&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; }<br>*使用vector&lt;string&gt;::size_type类型可以接收vector&lt;string&gt;类型的大小。<br>*不在for循环之前取得vector的大小，而在for循环中计算text.size()，是因为vector是可以增长的，所以它的元素数目是不确定的。<br>*和string对象一样，vector可以通过下标直接访问元素。<br><br><strong style="FONT-SIZE: 14pt">附表：vector操作</strong><br>
<table style="WIDTH: 460px; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=3 border=1>
    <tbody>
        <tr>
            <td>v.empty()</td>
            <td>如果v为空，则返回true</td>
        </tr>
        <tr>
            <td>v.size()</td>
            <td>返回v中元素的个数</td>
        </tr>
        <tr>
            <td>v.push_back(t)</td>
            <td>需按不过v的末尾增加一个值为t的元素</td>
        </tr>
        <tr>
            <td>v[n]</td>
            <td>返回v中位置为n的元素</td>
        </tr>
        <tr>
            <td>v1=v2</td>
            <td>吧v1的值与替换为v2中元素的副本</td>
        </tr>
        <tr>
            <td>v1==v2</td>
            <td>如果v1与v2相等，则返回true</td>
        </tr>
        <tr>
            <td>!=,&lt;,&lt;=,&gt;,&gt;=</td>
            <td>保持这些操作符惯有的含义</td>
        </tr>
    </tbody>
</table>
<img src ="http://www.cppblog.com/liujiajia/aggbug/51928.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-06-02 23:48 <a href="http://www.cppblog.com/liujiajia/archive/2008/06/02/51928.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++字符处理 cctype</title><link>http://www.cppblog.com/liujiajia/archive/2008/06/01/51853.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Sun, 01 Jun 2008 15:45:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/06/01/51853.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51853.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/06/01/51853.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51853.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51853.html</trackback:ping><description><![CDATA[cctype头文件中定义的函数：<br>
<table style="WIDTH: 490px; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=3 border=1>
    <tbody>
        <tr>
            <td>isalnum(c)</td>
            <td>如果c是字母或数字，则为true</td>
        </tr>
        <tr>
            <td>isalpah(c)</td>
            <td>如果c是字母，则为true</td>
        </tr>
        <tr>
            <td>iscntrl(c)</td>
            <td>如果c是控制字符，则为true</td>
        </tr>
        <tr>
            <td>isdigit(c)</td>
            <td>如果c是数字，则为true</td>
        </tr>
        <tr>
            <td>isgraph(c)</td>
            <td>如果c不是空格，则为true</td>
        </tr>
        <tr>
            <td>islower(c)</td>
            <td>如果c是小写字母，则为true</td>
        </tr>
        <tr>
            <td>isprint(c)</td>
            <td>如果c是可打印的字符，则为true</td>
        </tr>
        <tr>
            <td>ispunct(c)</td>
            <td>如果c是标点符号，则为true</td>
        </tr>
        <tr>
            <td>isspace(c)</td>
            <td>如果c是空白字符，则为true</td>
        </tr>
        <tr>
            <td>isupper(c)</td>
            <td>如果c是大些字母，则为true</td>
        </tr>
        <tr>
            <td>isxdigit(c)</td>
            <td>如果c是十六进制数，则为true</td>
        </tr>
        <tr>
            <td>tolower(c)</td>
            <td>如果c是大写字母，则返回小写字母形式，否则返回c。</td>
        </tr>
        <tr>
            <td>toupper(c)</td>
            <td>如果c是小写字母，则返回大些字母形式，否则返回c。</td>
        </tr>
    </tbody>
</table>
<img src ="http://www.cppblog.com/liujiajia/aggbug/51853.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-06-01 23:45 <a href="http://www.cppblog.com/liujiajia/archive/2008/06/01/51853.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>string对象的操作 size() empty() 关系运算 赋值 下标访问</title><link>http://www.cppblog.com/liujiajia/archive/2008/06/01/51849.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Sun, 01 Jun 2008 15:22:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/06/01/51849.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51849.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/06/01/51849.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51849.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51849.html</trackback:ping><description><![CDATA[<strong><em>string对象的操作：<br></em></strong><br><strong style="FONT-SIZE: 18pt">size():</strong>用来获取字符串的长度。<br>输出字符串s的长度：<br>&nbsp;&nbsp;&nbsp; string s("hello world!\n");<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"the size of '"&lt;&lt;s&lt;&lt;"' is"&lt;&lt;s.<strong>size</strong>()&lt;&lt;endl;<br><br>判断字符串是否为空的两种方式：<br>&nbsp;&nbsp;&nbsp; if(s.<strong>size</strong>()==0)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;"s.size==0"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; if(s.<strong>empty</strong>())<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;"s.empty()"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; }<br><br><strong style="FONT-SIZE: 14pt">获得字符串长度：</strong><br>&nbsp;&nbsp;&nbsp; int num=s.size();&nbsp;&nbsp;&nbsp; 使用整形接收字符串长度可能会有问题，因为int变量的表示范围太小，有时不能存储string对象的长度。使用string::size_type类型可以满足要求，这种类型是unsigned的，这比signed类型要大一倍。事实上size()函数返回的也是 size_type 类型。<br><br><span style="FONT-SIZE: 14pt"><strong>关系运算：<br></strong></span>可以使用：s1==s2；s1&gt;s2；s1&lt;s2，来比较两个字符串。<br><br><strong style="FONT-SIZE: 14pt">赋值运算：</strong><br>&nbsp;&nbsp;&nbsp; string snew="empty";<br>&nbsp;&nbsp;&nbsp; snew=s1;<br>上面代码首先初始化一个snew字符串，第二句将snew占用的内存释放掉，然后给snew足够存放s1副本的内存空间，最后把s1中所有的字符复制到新分配的内存空间。<br><br><span style="FONT-SIZE: 14pt"><strong>字符串连接：</strong></span><br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s1+"."&lt;&lt;endl;&nbsp;&nbsp;&nbsp; OK<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"1"+s1&lt;&lt;endl;&nbsp;&nbsp; OK<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s1+s2&lt;&lt;endl;&nbsp;&nbsp;&nbsp; OK<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;s1+s2+"."&lt;&lt;endl; OK<br>&nbsp;&nbsp;&nbsp; //cout&lt;&lt;"hello"+"jiajia"&lt;&lt;endl;&nbsp; ERROR<br>当进行string对象和字符串字面值混合连接操作时，+操作符的左右操作数必须至少有一个是string类型的。<br><br><strong style="FONT-SIZE: 14pt">通过下标访问string对象：</strong><br>&nbsp;&nbsp;&nbsp; string对象下标从0开始。<br>&nbsp;&nbsp;&nbsp; for(string::size_type ix=0;ix&lt;s1.size();ix++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s1[ix]+=ix;<br>&nbsp;&nbsp;&nbsp; }
<img src ="http://www.cppblog.com/liujiajia/aggbug/51849.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-06-01 23:22 <a href="http://www.cppblog.com/liujiajia/archive/2008/06/01/51849.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符串的读取 cin 和 getline()</title><link>http://www.cppblog.com/liujiajia/archive/2008/06/01/51716.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Sat, 31 May 2008 16:02:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/06/01/51716.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51716.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/06/01/51716.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51716.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51716.html</trackback:ping><description><![CDATA[<span style="FONT-SIZE: 18pt"><strong>cin</strong></span><br>&nbsp;&nbsp;&nbsp; string s;<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"Enter some words...";<br>&nbsp;&nbsp;&nbsp; <strong>cin</strong>&gt;&gt;s;<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"You write:"&lt;&lt;s&lt;&lt;endl;<br>*读取并忽略开头所有的空白字符（空格、换行符、制表符）。<br>*读取字符直至遇到空白字符，读取终止。<br><br><span style="FONT-SIZE: 18pt"><strong>getline()</strong></span><br>&nbsp;&nbsp;&nbsp; string line;<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"Enter a line...";<br>&nbsp;&nbsp;&nbsp; getline(cin,line);<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"You write:"&lt;&lt;line&lt;&lt;endl;<br>*这个函数接收两个参数，第一个是输入流对象，第二个是string对象。<br>*getline不忽略行开头的换行符，只要getline()遇到换行符，哪怕是第一个字符，getline也将停止读取字符并返回。<br>*能够读取除换行之外的空白字符。<br><br>
<img src ="http://www.cppblog.com/liujiajia/aggbug/51716.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-06-01 00:02 <a href="http://www.cppblog.com/liujiajia/archive/2008/06/01/51716.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++初始化字符串的方式</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/31/51714.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Sat, 31 May 2008 15:48:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/31/51714.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51714.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/31/51714.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51714.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51714.html</trackback:ping><description><![CDATA[C++初始化字符串的方式很灵活：<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">iostream</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">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<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">string</span><span style="COLOR: #000000">&nbsp;s1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;s2(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">hello</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);&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;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;s3(s2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">将s3初始化为一个s2的副本&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;s4</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">hi&nbsp;jiajia</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;&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">string</span><span style="COLOR: #000000">&nbsp;s5(</span><span style="COLOR: #000000">15</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;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s1</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s2</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s3</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s4</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s5</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</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>}</span></div>
<br>注意：由于历史原因以及为了与C语言兼容，字符串字面值与标准库string类型不是同一种类型。
<img src ="http://www.cppblog.com/liujiajia/aggbug/51714.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 23:48 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/31/51714.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>命名空间的using声明</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/31/51713.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Sat, 31 May 2008 15:36:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/31/51713.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51713.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/31/51713.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51713.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51713.html</trackback:ping><description><![CDATA[std::cout&lt;&lt;s&lt;&lt;endl;<br>&nbsp;: : 操作符是作用域解析符。它的含义是右操作数的名字可以在左操作数的作用域中找到。std::cout 的意思是说所需名字cin是在命名空间std中定义的。这种符号引用标准库名字的方式是非常麻烦的。<br>&nbsp;&nbsp;&nbsp;&nbsp; c++提供了一种简单的方式来使用命名空间成员，using声明。这是一种最安全的机制。<br>&nbsp;
<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: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;std::cin;<br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;std::endl;<br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;std::</span><span style="COLOR: #0000ff">string</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">string</span><span style="COLOR: #000000">&nbsp;s;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">正确。string使用了using声明</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">s;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">正确。cin使用了using声明</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">错误。cout没有使用using声明</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;std::cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;&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;system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);&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><em><strong>命名空间扩展：</strong></em><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在标准C++以前，都是用#include&lt;iostream.h&gt;这样的写法的，因为要包含进来的头文件名就是iostream.h。标准C++引入了名字空间的概念，并把iostream等标准库中的东东封装到了std名字空间中，同时为了不与原来的头文件混淆，规定标准C++使用一套新的头文件，这套头文件的文件名后不加.h扩展名，如iostream、string等等，并且把原来C标准库的头文件也重新命名，如原来的string.h就改成cstring(就是把.h去掉，前面加上字母c)，所以头文件包含的写法也就变成了#include &lt;iostream&gt;。 <br>并不是写了#include&lt;iostream&gt;就必须用using namespace std;我们通常这样的写的原因是为了一下子把std名字空间的东东全部暴露到全局域中（就像是直接包含了iostream.h这种没有名字空间的头文件一样），使标准C++库用起来与传统的iostream.h一样方便。如果不用using namespace std;使用标准库时就得时时带上名字空间的全名，如std::cout &lt;&lt; "hello" &lt;&lt; std::endl; <br>&nbsp;&nbsp;&nbsp;&nbsp; #include "iostream"与#include&lt;iostream&gt;的区别：前者先在当前目录找iostream文件，找不到再去系统头文件路径找，后者反之。因此，做为一个良好的习惯，在包含系统头文件时尽量用&lt;&gt;，而在包含自己的工程中的头文件时用""。
<img src ="http://www.cppblog.com/liujiajia/aggbug/51713.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 23:36 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/31/51713.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>开始学习c++语言</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/31/51710.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Sat, 31 May 2008 15:07:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/31/51710.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51710.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/31/51710.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51710.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51710.html</trackback:ping><description><![CDATA[今天c语言的学习将告一段落。开始学习c++语言。<br>本来学习c就是为了学c++而准备的，但是感觉进度有点慢了，看了些基础的，准备学c++了。<br><br>对c++语言的学习，我将用那本经典的《C++ Primer》(中文版)。另外编辑器使用Dev-c++4.9.9.2。 <br><br>另外，由于对C语言的学习和以前C++的一点基础，所以选择了《C++ Primer》这本书。但是，如果没有这方面的基础，建议看《C++ Primer Plus》这本书。
<img src ="http://www.cppblog.com/liujiajia/aggbug/51710.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 23:07 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/31/51710.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><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>1</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>1</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>字符串的输入和输出（认识gets()fgets()scanf()和puts()fputs()printf()）</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/27/51304.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Tue, 27 May 2008 10:03:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/27/51304.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/51304.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/27/51304.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/51304.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/51304.html</trackback:ping><description><![CDATA[<span style="FONT-SIZE: 18pt"><strong>字符串输入：</strong></span><br><strong><em>gets()</em></strong><br>&nbsp;&nbsp;&nbsp;&nbsp;char name[20];<br>&nbsp;&nbsp;&nbsp; printf("Hi,What's your name?\n");<br>&nbsp;&nbsp;&nbsp; gets(name);<br>&nbsp;&nbsp;&nbsp; printf("Nice name %s",name);<br>它使用一个地址把字符串赋予name。<br><br>&nbsp;&nbsp;&nbsp; char name[20];<br>&nbsp;&nbsp;&nbsp; char *p;<br>&nbsp;&nbsp;&nbsp; printf("\nEn,What's your name?\n");<br>&nbsp;&nbsp;&nbsp; p=gets(name);<br>&nbsp;&nbsp;&nbsp; printf("%s?Oh,Nice name %s\n",name,p);<br>get()的代码使用return 关键字返回字符串的地址，程序把这个地址分配给指针p。<br><br><em><strong>fgets()</strong></em><br>fgets()是为文件I/O而设计的，处理键盘输入不是特别方便。<br><br>&nbsp;&nbsp;&nbsp; printf("\nHi,What's your name?\n");<br>&nbsp;&nbsp;&nbsp; p=fgets(name,20,stdin);<br>&nbsp;&nbsp;&nbsp; printf("%s?Oh,Nice name %s\n",name,p);<br>*fgets()的第二个参数说明最大读入的字符数。如果这个参数值为n，那么fgets()就会读取最多n-1个字符或读完一个换行符为止。两个条件满足任意一个结束。<br>*fgets()读取到换行符，就会把它存到字符串里，而不是想gets()那样丢弃它。<br>*fgets()的第三个参数说明读哪个文件。从键盘上读数据时，可以使用stdin(代表standard input)作为参数。<br><br><strong><em>scanf()</em></strong><br>&nbsp;&nbsp;&nbsp; char name1[11], name2[11];<br>&nbsp;&nbsp;&nbsp; int count;<br>&nbsp;&nbsp;&nbsp; printf("\nPlease write down 2 names...\n");<br>&nbsp;&nbsp;&nbsp; count=scanf("%5s %6s",name1,name2);<br>&nbsp;&nbsp;&nbsp; printf("\nname1:%s\nname2:%s",name1,name2);<br><br>scanf()允许指定输入字符串长度等格式。上面的程序如果输入"liujiajia liujiajia",程序将输出"name1:liuji&nbsp;&nbsp; name2:liujia";<br><br><span style="FONT-SIZE: 18pt"><strong>字符串输出：</strong></span><br><em><strong>puts()</strong></em><br>&nbsp;&nbsp;&nbsp; char str[15]="hello world";<br>&nbsp;&nbsp;&nbsp; const char *str2="HELLO WORLD";<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; puts(str);<br>&nbsp;&nbsp;&nbsp; puts(str2);<br>&nbsp;&nbsp;&nbsp; puts(&amp;str[5]);<br>&nbsp;&nbsp;&nbsp; puts(str2+2);<br>puts()显示字符串时自动在其后添加一个换行符。<br>puts(&amp;str[5]);将输出从str的第六个元素开始到字符串结束。<br>puts(str2+2);将输出从str2的地址向后移动两个字符开始到字符串结束。<br><br><strong><em>fputs()</em></strong><br>&nbsp;&nbsp;&nbsp; fputs(str,stdout);<br>&nbsp;&nbsp;&nbsp; fputs(str2,stdout);<br>&nbsp;&nbsp;&nbsp; fputs(&amp;str[5],stdout);<br>&nbsp;&nbsp;&nbsp; fputs(str2+2,stdout);<br>*fputs()第二个参数表示要写的文件。可以使用stdout(代表standard output)作为参数。<br>*fputs()不自动输出换行符，这与puts()不太相同。<br><br><strong><em>printf()</em></strong><br>这个不用多说了。
<img src ="http://www.cppblog.com/liujiajia/aggbug/51304.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 18:03 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/27/51304.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>使用igoogle</title><link>http://www.cppblog.com/liujiajia/archive/2008/05/15/49882.html</link><dc:creator>刘加加</dc:creator><author>刘加加</author><pubDate>Wed, 14 May 2008 16:00:00 GMT</pubDate><guid>http://www.cppblog.com/liujiajia/archive/2008/05/15/49882.html</guid><wfw:comment>http://www.cppblog.com/liujiajia/comments/49882.html</wfw:comment><comments>http://www.cppblog.com/liujiajia/archive/2008/05/15/49882.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liujiajia/comments/commentRss/49882.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liujiajia/services/trackbacks/49882.html</trackback:ping><description><![CDATA[大家有没有用过google的自定义主页，就是 <a title=igoogle href="http://www.google.com/ig" target=_blank>igoogle</a>&nbsp;。可以自己添加google为你提供的小工具来组成你的主页。里面有个google眼睛，感觉很好玩。<br>可是我同时想把我一些常用的链接放进去，但是igoogle并没有提供相关的工具，怎么办？没关系，google为你提供了小工具开发的接口。<br><br>1.进入 <a title=igoogle href="http://www.google.com/ig" target=_blank>igoogle</a>&nbsp;。<br>2.点击&#8220;添加内容&#8221;链接。<br>3.点击&#8220;添加供稿源或小工具&#8221;，输入&#8216;http://helloliukai.googlepages.com/hi.xml&#8217; 点击添加。<br>4.然后回到首页，你可以看到你的igoogle主页上添加了一个名为&#8216;hello world example&#8217;的小工具。<br><br>呵呵，你只要打开<a href="http://helloliukai.googlepages.com/hi.xml">http://helloliukai.googlepages.com/hi.xml</a> 这个xml文件就会发现其中的问题了。<br>igoogle通过解析xml文件来向用户展示小工具。你只要把&#8220;&lt;![CDATA[Hello, world!]]&gt;&#8221;中的&#8220;Hello, world!&#8221;替换成任意html或javascript代码就行了。然后把修改过的xml文件上传到一个公共的空间里面就行了。我用的同样是google的产品，Page Creator：<a href="http://pages.google.com/">http://pages.google.com/</a>&nbsp;。呵呵。google的东东确实很多。<br>另外，xml确实也很强大。<br><br><a href="http://code.google.com/intl/zh-CN/apis/gadgets/">http://code.google.com/intl/zh-CN/apis/gadgets/</a>&nbsp;这里有google的资源，有教程和小编辑器。但教程感觉已经过期了，好多里面的代码都不能用。可以使用那个小编辑器打开各种模板。
<img src ="http://www.cppblog.com/liujiajia/aggbug/49882.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 00:00 <a href="http://www.cppblog.com/liujiajia/archive/2008/05/15/49882.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>