??xml version="1.0" encoding="utf-8" standalone="yes"?>久久精品免费网站网,国产成人无码久久久精品一,欧美一区二区三区久久综http://www.shnenglu.com/liujiajia/学习、工作、生z,q是三个问题?/description>zh-cnSun, 29 Jun 2025 07:45:59 GMTSun, 29 Jun 2025 07:45:59 GMT60最单的windowsE序http://www.shnenglu.com/liujiajia/archive/2008/07/23/56963.html刘加?/dc:creator>刘加?/author>Wed, 23 Jul 2008 09:01:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/07/23/56963.htmlhttp://www.shnenglu.com/liujiajia/comments/56963.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/07/23/56963.html#Feedback3http://www.shnenglu.com/liujiajia/comments/commentRss/56963.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/56963.html1.打开vc++;
2.选择菜单栏file----newQ?br>3.选项卡选择ProjectsQ?br>4.模板选择 Win32 Application;
5.输入Project nameQ?br>6.Loction 选择路径Q?br>7.点击OKl箋Q?br>8.选择默认?#8220;An Empty project”Q?br>9.点击finish完成创徏工程Q?br>10.向工E里面添加一个c++文gQ(file--new--选项卡file--C++ source file--输入文g?-点击OKQ?br>11.输入以下代码Q?/p>
#define WIN32_LEAN_AND_MEAN

#include
<windows.h>
#include
<windowsx.h>

int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdshow)
{
    MessageBox(NULL,
"Hello World!","first win",MB_OK|MB_ICONEXCLAMATION);
    
return 0;
}

12.保存Q按ctrl+F5~译q运行?br>13.弹出一个对话框?br>

      以上是L世界上最单的WindowsE序Q?br>       



]]>
函数q回值的Ҏ情况Q返回引用、非引用、引用左|http://www.shnenglu.com/liujiajia/archive/2008/07/16/56244.html刘加?/dc:creator>刘加?/author>Tue, 15 Jul 2008 16:41:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/07/16/56244.htmlhttp://www.shnenglu.com/liujiajia/comments/56244.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/07/16/56244.html#Feedback2http://www.shnenglu.com/liujiajia/comments/commentRss/56244.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/56244.htmlq回非引?/span>

      string make_plural(size_t i,const string &word,const string &ending)
      {
             return (i==1)?word:word+ending;
      }

      以上函数Q当i{于1Ӟ函数q回word形参的副本;当i不等?时函数返回一个时的string对象Q这个时对象是由字W串word和ending相加而成的。这两种情况下,return都在调用该函数的地方防复制了q回的string对象?br>
q回引用

      const string &shorterString(const string &s1,const string &s2)
      {
             return s1.size()<s2.size()?s1:s2;
      }

      以上函数的返回值是引用cd。无回s1或是s2,调用函数和返回结果时Q都没有复制q些string对象?br>
千万不要q回局部对象的引用

      const string &mainip(const string &s)
      {
             string ret=s;
             return ret;
      }
      
      当函数执行完毕,E序释攑ֈ配给局部对象的存储I间。此Ӟ对局部对象的引用׃指向不确定的内存。(我在dev c++里运行通过。。。?但是vc6和vc2008不能~译通过Q。同理,也不能返回局部对象的指针?br>      

引用q回左|比较奇妙Q?/strong>

      char &get_val(string &str,string::size_type ix)
      {
             return str[ix];
      }

      使用语句调用:
       string s("123456");
       cout<<s<<endl;
       get_val(s,0)='a';
       cout<<s<<endl;
      把函数应用于左|q是W一ơ见刎ͼ真是孤陋寡闻Q?br>
以下是上面几U情늚实例代码Q?br>
//liujiajia
//2008-7-16
#include<iostream>
using namespace std;
string make_plural(size_t,const string&,const string&);
const string &shorterString(const string &,const string &);
const string &mainip(const string&);
char &get_val(string &,string::size_type);
int main(void)
{
    cout
<<make_plural(1,"dog","s")<<endl;
    cout
<<make_plural(2,"dog","s")<<endl;
    
    
string string1="1234";
    
string string2="abc";
    cout
<<shorterString(string1,string2)<<endl;
    
    cout
<<mainip("jiajia")<<endl;
    
    
    
string s("123456");
    cout
<<s<<endl;
    get_val(s,
0)='a';
    
    cout
<<s<<endl;
    
    getchar();
    
return 0;
}
//q回非引?nbsp;
string make_plural(size_t i,const string &word,const string &ending)
{
    
return (i==1)?word:word+ending;
}
//q回引用 
const string &shorterString(const string &s1,const string &s2)
{
    
return s1.size()<s2.size()?s1:s2;
}
//止q回局部对象的引用Q我的dev c++ 没有报错Q比较可怕) 
const string &mainip(const string &s)
{
    
string ret=s;
    
return ret;
}
//引用q回左|W一ơ听_ 
char &get_val(string &str,string::size_type ix)
{
    
return str[ix];
}



]]>
数组的Ş?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/07/15/56143.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/56143.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/07/15/56143.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/56143.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liujiajia/services/trackbacks/56143.html</trackback:ping><description><![CDATA[      数组有两个特D的性质Q媄响我们定义和使用作用在数l上的函敎ͼ一是不能复制数l。二是用数l名字时Q数l会自动转换为指向其W一个元素的指针。因为数l不能复Ӟ所以无法编写用数l类型Ş参的函数。因为数l会被初始化为指针,所以处理数l的函数通常通过操作指向数组中的元素的指针来处理数组?br><br>CZQ?br><br>      //形式1<br>      void printValues(const int ia[10])     //虽然形参指定了数l的大小Q但~译时不会检查实参数l大,因ؓ~译器只查Ş参是否是指针Qƈ且指针类型和数组元素的类型是否匹配?br>      {<br>             for(size_t i=0;i!=10;i++)<br>             {<br>                    cout<<ia[i]<<endl; <br>             }<br>      }<br>      //形式2<br>      void printValues2(const int ia[],int size)<br>      {<br>             for(size_t i=0;i!=size;i++)<br>             {<br>                    cout<<ia[i]<<endl; <br>             }<br>      }<br>      int main(void)<br>      {<br>            int i[10]={1,2,3,4,5,6,7,8,9,0};<br>            printValues(i);   //i作ؓprintValues的实参,在这里ؓ数组i的首地址Q或者说他{换ؓ成一个指?br>            printValues2(i,10);<br>      }<br><br>      数组形参可以定义为引用或非引用类型。大部分情况下,数组以普通的非引用类型传?此时数组会悄悄的转换为指针。一般来_非引用类型的形参会初始化为其相应实参的副本。但是在传递数l时Q实参是指向数组W一个元素的指针QŞ参复制的是这个指针的|而不是数l元素本w。函数操作的也是指针的副本,因此不会修改实参指针的倹{然而,函数可通过该指针修改它所指向的数l元素的倹{?br><br>以上代码可以使用指针表示Q效果等同:<br><br>      void printValues3(const int *ia,int size)<br>      {<br>             for(size_t i=0;i!=size;i++,ia++)<br>             {<br>                 cout<<*ia<<endl; <br>             }<br>       }<br><br><br>CZ2Q通过引用传递数l)Q?br><br>      void printValues4(int (&arr)[10])    //q种情况下,数组大小成ؓ形参和实参的一部分。编译器会检查数l大是否匹配?nbsp;int (&arr)[10] 中的括h必须的?br>      {<br>          for(size_t i=0;i!=10;i++)<br>          {<br>                 cout<<arr[i]<<endl; <br>          }<br>      }<br>      int main(void)<br>      {<br>            printValues4(i);<br>      }<br><br>      和其他类型一P数组形参可声明ؓ数组的引用。如果Ş参是数组的引用,~译器不会将数组实参转化为指针,而是传递数l的引用本n? <img src ="http://www.shnenglu.com/liujiajia/aggbug/56143.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-07-15 00:05 <a href="http://www.shnenglu.com/liujiajia/archive/2008/07/15/56143.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>传递指向指针的引用http://www.shnenglu.com/liujiajia/archive/2008/07/14/56135.html刘加?/dc:creator>刘加?/author>Mon, 14 Jul 2008 14:19:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/07/14/56135.htmlhttp://www.shnenglu.com/liujiajia/comments/56135.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/07/14/56135.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/56135.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/56135.html
//liujiajia
//2008-7-14 
#include<iostream>
using namespace std;
void ptrswap(int *&,int *&);
int main(void)
{
    
int i=10;
    
int j=20;
    
int *pi=&i;
    
int *pj=&j;
    
    cout
<<"调用ptr()之前:pi="<<pi<<",*pi="<<*pi<<";pj="<<pj<<",*pj="<<*pj<<endl;
    ptrswap(pi,pj);
    cout
<<"调用ptr()之后:pi="<<pi<<",*pi="<<*pi<<";pj="<<pj<<",*pj="<<*pj<<endl;
    
    getchar();
    
return 0;
}
void ptrswap(int *&v1,int *&v2)
{
    
int *temp=v1;
    v1
=v2;
    v2
=temp;
}

在调用ptrswap之前Qpi指向i,pj指向j。在调用ptrswap之后Qpi指向j,而pj指向了i?

]]>
使用引用形参的优点和注意事项http://www.shnenglu.com/liujiajia/archive/2008/07/14/56131.html刘加?/dc:creator>刘加?/author>Mon, 14 Jul 2008 13:48:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/07/14/56131.htmlhttp://www.shnenglu.com/liujiajia/comments/56131.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/07/14/56131.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/56131.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/56131.html使用引用形参的优点:

      在向函数传递大对象Ӟ需要用引用Ş参。虽然赋值实参对于内|数据类型的对象或者规模较的cȝ型来说没有什么问题,但是对于大部分的cȝ型或者大型数l,它的效率比较低了。另外,某些cȝ型是无法复制的。用引用Ş参,函数可以直接讉K实参对象Q而无d制它?br>
CZQ?br>      bool isShorter(const string &str1,const string &str2)
      {
             return str1.size()<str2.size();
      }
      ׃string对象可能非常的长Q我们希望避免复制操作?br>      如果使用引用形参的唯一目的是避免复制Ş参,则应Ş参定义ؓconst引用?br>
使用引用形参应该注意的:

      int incr(int &val)
      {
          return ++val;
      }
      void main(void)
      {
             short v1=1;
             const int v2=10;
             int v3=20;

             incr(v1);      //错误Qv1不是int
             incr(v2);      //错误Qv2是const
             incr(v3);      //正确
             incr(10);      //错误      
      }

   所以,调用非constcd的引用Ş参,实参必须不是constcd的,而且实参的类型和形参的类型应当一致?/p>

]]>
使用引用形参q回额外的信?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/07/14/56085.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/56085.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/07/14/56085.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/56085.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liujiajia/services/trackbacks/56085.html</trackback:ping><description><![CDATA[      一个函数只能有一个返回|但如果我惌他返回更多的信息怎么办呢Q这U情况可以用引用Ş参?br>      因ؓ引用形参在函数调用后是可以修改实参值的Q所以就相当与函数的q回倹{?br><br>      下面的代码演CZ一个带有一个返回|q过引用形参向主调函数返回信息的函数。这个函数的功能是找出在容器中某一个数值出现的ơ数Qƈq回该数值第一ơ出现的q代器?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"><</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br>vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::const_iterator find_val(vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::const_iterator,vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::const_iterator,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">,vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::size_type </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>    vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"> some;<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> val;<br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">使用push_back()输入容器中的?nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(cin</span><span style="COLOR: #000000">>></span><span style="COLOR: #000000">val)<br>    {<br>        some.push_back(val);<br>    }<br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">存储值出现的ơ数 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::size_type ccurs;<br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">调用函数 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::const_iterator 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>    <br>    cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">ccurs</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br>    <br>    getchar();<br>}<br><br>vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::const_iterator find_val(<br>    vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::const_iterator beg,<br>    vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::const_iterator end,<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> value,<br>    vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::size_type </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">occurs)<br>{<br>    vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::const_iterator res_iter</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">end;<br>    occurs</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>    </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>    {<br>        </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>        {<br>            </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">q个if语句的作用是让res_iter{于值第一ơ出现的位置 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">            </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>            {<br>                res_iter</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">beg;<br>            }<br>            occurs</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>        }<br>    }<br>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> res_iter;<br>}</span></div> <br><br>       <img src ="http://www.shnenglu.com/liujiajia/aggbug/56085.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-07-14 12:12 <a href="http://www.shnenglu.com/liujiajia/archive/2008/07/14/56085.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>函数的Ş参(非引用Ş参、指针Ş参、引用Ş参)http://www.shnenglu.com/liujiajia/archive/2008/07/14/56054.html刘加?/dc:creator>刘加?/author>Sun, 13 Jul 2008 16:30:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/07/14/56054.htmlhttp://www.shnenglu.com/liujiajia/comments/56054.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/07/14/56054.html#Feedback2http://www.shnenglu.com/liujiajia/comments/commentRss/56054.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/56054.html
      void add1(int v1)
      {
          v1+=1;
      }

      q是最普通的形参方式Q当函数被调用时Q实参的副本初始化Ş参,函数q没有访问调用所传递的实参Q因此v1+=1不会修改实参的倹{对v1的操作只是修改了实参的一个副本?br>
二、指针Ş?br>
      void add2(int *p)
      {
          *p+=1;
          p+=1;
      } 

      使用指针做ؓ函数的Ş参,同样指针的|指针的地址Q不会因为p+=1而受到媄响,但是指针指向的地址的|*pQ将会改变。所以要想修改实参的|可以使用q种Ҏ。但是还有一U更安全更自然的Ҏ-引用形参

三、引用Ş?br>
      void swap(int &a,int &b)
      {
          int temp=a;
          a=b;
          b=temp;
       }

      引用形参直接兌到其所l定的对象,而非q些对象的副本?所以这U方法可以修改实参的|而且更加直观?br>
三种Ҏ实例代码Q?br>
//liujiajia
//2008-7-14 
#include<iostream>
using namespace std;
void add1(int);
void add2(int*);
void swap(int &,int &);
int main(void)
{
    
int n=10;
    cout
<<"调用add1()之前n="<<n<<endl;
    add1(n);
    cout
<<"调用add1()之后n="<<n<<endl;
    
    
int *p=&n;
    
    cout
<<"调用add2()之前n="<<n<<",p="<<p<<endl;
    add2(p);
    cout
<<"调用add2()之后n="<<n<<",p="<<p<<endl;
    
    
int a=1,b=2;
    cout
<<"调用swap()之前a="<<a<<",b="<<b<<endl;
    swap(a,b);
    cout
<<"调用swap()之后a="<<a<<",b="<<b<<endl;
    
    getchar();
}

//非引用Ş?nbsp;
void add1(int v1)
{
    v1
+=1;
}

//指针形参
void add2(int *p)
{
    
*p+=1;
    p
+=1;


//引用形参
void swap(int &a,int &b)
{
    
int temp=a;
    a
=b;
    b
=temp;
}


]]>
创徏和用动态数l?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/07/12/55984.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/55984.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/07/12/55984.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/55984.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liujiajia/services/trackbacks/55984.html</trackback:ping><description><![CDATA[      动态数l,֐思义Q就是可以动态分配长度的数组。在c语言中,数组的长度是固定的,而这U限制显然是很不灉|的。在c++中,数组有时候不用在~译时就知道长度Q直到运行时才知道。与普通的数组变量不同Q动态分配的数组一直存在,直到E序昑ּ的释攑֮为止?br><br><strong style="FONT-SIZE: 14pt">定义Q?/strong><br><br>      int *p=new int[20];<br><br>      p是一个动态数l,q时我们发现Q其实所谓的动态数l就是一个指针,使用new为p分配长度。我们可以通过q种方式初始化: int *p=new int[20]();<br><br>      同样上面的语句等同于Q?br>      int n=20; <br>      int *p=new int[n];<br>      因ؓn是一个变量,所以数l的长度是动态的?br><br><strong style="FONT-SIZE: 14pt">操作实例Q?/strong><br><br>      int n=20; <br>      int *p=new int[n];<br><br>      p可以使用数组下标为其赋|<br>      for(int i=0;i<n;i++)<br>      {<br>            p[i]=i+3;<br>      }<br>     上面q种方式没有问题Q但我想既然p是一个指针,那么使用指针操作一样也可以吧?<br><br>     for(int i=0;i<n;i++,p++)<br>    {<br>           *p=i;<br>     }<br> <br>      q样可以了Q将他们输出Q(q时指针p又指向了最后)<br>      <br>      p--;<br>      for(int *p2=p-10;p2!=p;p--)<br>      {<br>           cout<<*p<<endl;<br>      }<br><br><span style="FONT-SIZE: 14pt"><strong>动态空间释放:</strong></span><br><br>      最后重要的是别忘了动态创造的I间释放掉,语句是:delete [] p;   ‘[ ]’表明该指针是指向的自由存储区的数l,而非单个对象。如果遗漏了I方括号Q编译器无法发现这个错误,导致程序在q行时出错?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"><</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">;<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">[n];<br>    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</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"><</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>    {<br>        cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br>        </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br>    }<br>    p</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br>    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> </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>    {<br>        cout</span><span style="COLOR: #000000"><<*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br>    }<br>    <br>    delete [] p;<br>    <br>    getchar();<br>}</span></div> <br> <img src ="http://www.shnenglu.com/liujiajia/aggbug/55984.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-07-12 23:26 <a href="http://www.shnenglu.com/liujiajia/archive/2008/07/12/55984.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>const和指针(指向const对象的指针、const指针Q?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/06/21/54166.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/54166.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/06/21/54166.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/54166.html</wfw:commentRss><trackback:ping>http://www.shnenglu.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>q个p是一个指向intcdconst对象的指针,const限定了指针p所指向的类型,而ƈ非p本n。也是说p本nq不是const。在定义时不需要对它进行初始化Q还可以lp重新赋|使其指向另一个const对象。但不能通过p修改所指向对象的倹{?br>CZ1Qint a=0; p=&a;  可以?br>CZ2Q?p=20;  不可以?br>l论Q这U指向const对象的指针只是限制不能修改p指向对象的数|而不是限制p指向什么对象?br><br>把一个const对象的地址赋给一个不是指向const对象的指针也是不行的?br>CZ3Qconst int b=10;<br>               int *p2=&b;   //error<br>               const int *p3=&b; //ok<br>l论Q因为变量b有const修饰Q不能被修改。但指针p2是一个普通的指针Q可以修Ҏ向对象的|两种声明矛盾Q所以不合法。而指向const对象的指针不允许修改指针指向对象的数|所以这U方式合法?br><br><span style="FONT-SIZE: 14pt"><strong>2.const指针</strong></span><br><br>int c=20;<br>int *const p4=&c;<br><br>指针p4UCؓconst指针。它和指向const对象的指针恰好相反,它不能够修改所指向对象Q但却能够修Ҏ向对象的数倹{另外,q种指针在声明时必须初始化?br><br><span style="FONT-SIZE: 14pt"><strong>3.指向const对象的const指针</strong></span><br><br>const int d=30;<br>const int *const dp=&d;<br><br>指针dp既不能修Ҏ向的对象Q也不能修改只想对象的倹{? <img src ="http://www.shnenglu.com/liujiajia/aggbug/54166.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-06-21 00:13 <a href="http://www.shnenglu.com/liujiajia/archive/2008/06/21/54166.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>标准?bitset cdhttp://www.shnenglu.com/liujiajia/archive/2008/06/06/52400.html刘加?/dc:creator>刘加?/author>Fri, 06 Jun 2008 15:04:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/06/06/52400.htmlhttp://www.shnenglu.com/liujiajia/comments/52400.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/06/06/52400.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/52400.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/52400.html
bitset对象的定义和初始?br>
bitset<n> b;               b有n位,每位都是0Q?br>bitset<n> b(u);           b是unsigned long型u的副本;
bitset<n> b(s);            b是string对象s中含有的位串的副本;
bitset<n> b(s,pos,n);   b是s中从位置pos开始的n个位的副本;

CZQ?br>    bitset<16> bitvec1(0xffff);         //bitvec1?-15位都?
    bitset<32> bitvec2(0xffff);         //bitvec2?-15为是1Q?6-31?
    bitset<128> bitvec3(0xffff);      //bitvec3?-15位是1Q?6-127都是0
    
    string str("1100");
    bitset<32> bitvec4(str);            //bitvec4的第2、第3位是1Q其余都?。因为string对象和bitset对象之间是反向{化的Qstring对象的最双字符Q即下标最大的那个字符Q用来初始化bitset对象的低价位Q即标?的位Q?br>
bitset对象的操作:

b.any()         b中是否存在置?的二q制位?
b.none()       b中不存在|ؓ1的二q制位吗Q?br>b.count()      b中gؓ1的二q制位的个数
b.size()         b中二q制位的个数
b[pos]          讉Kb中在pos处的二进制位
b.test(pos)    b中在pos处的二进制位是否?Q?br>b.set()           把b中所有的二进制位都置?
b.set(pos)      把b中在pos处的二进制位|ؓ1
b.reset()        把b中所有的二进制位都置?
b.reset(pos)    把b中在pos处的二进制位|ؓ0
b.flip()            把b中所有二q制位逐位取反
b.flip(pos)      把b中在pos处的二进制位取反
b.to_ulong()   用b中同L二进制位q回一个unsigned long?br>os<<b            吧b中的位集输出到os?br>

]]>
q代?iteratorhttp://www.shnenglu.com/liujiajia/archive/2008/06/06/52399.html刘加?/dc:creator>刘加?/author>Fri, 06 Jun 2008 14:32:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/06/06/52399.htmlhttp://www.shnenglu.com/liujiajia/comments/52399.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/06/06/52399.html#Feedback3http://www.shnenglu.com/liujiajia/comments/commentRss/52399.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/52399.htmlq代?/strong>(iterator)是一中检查容器内元素q历元素的数据cd?br>
每种容器cd都定义了自己的P代器cdQ如vector:
vector<int>::iterator iter;q条语句定义了一个名为iter的变量,它的数据cd是由vector<int>定义的iteratorcd?br>
使用q代器读取vector中的每一个元素:
   vector<int> ivec(10,1);
   
    for(vector<int>::iterator iter=ivec.begin();iter!=ivec.end();++iter)
    {
        *iter=2;      //使用 * 讉Kq代器所指向的元?br>    }


const_iterator:
只能d容器中的元素Q而不能修攏V?br>     for(vector<int>::const_iterator citer=ivec.begin();citer!=ivec.end();citer++)
    {
        cout<<*citer;
        //*citer=3;   error
    }

vector<int>::const_iterator ?const vector<int>::iterator的区?br>    const vector<int>::iterator newiter=ivec.begin();
    *newiter=11;   //可以修改指向容器的元?br>    //newiter++;     //q代器本w不能被修改

iterator的算术操作:
iterator除了q行++,--操作Q可以将iter+n,iter-n赋给一个新的iteraor对象。还可以使用一个iterator减去另外一个iterator.
    const vector<int>::iterator newiter=ivec.begin();
    vector<int>::iterator newiter2=ivec.end();
    cout<<"\n"<<newiter2-newiter;

]]>
认识标准库vectorcdhttp://www.shnenglu.com/liujiajia/archive/2008/06/02/51928.html刘加?/dc:creator>刘加?/author>Mon, 02 Jun 2008 15:48:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/06/02/51928.htmlhttp://www.shnenglu.com/liujiajia/comments/51928.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/06/02/51928.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/51928.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/51928.htmlvector 是一个类模板(class template)?br>
vector的定义和初始化:
    vector<string> text;            //保存cd为string的对象,默认构造函数text为空
    vector<string> text2(10);   //保存cd为string的对象,包含10个空字符?br>    vector<int> ive(10,9);         //保存int的对象,保存10个gؓ9的整形变?br>    vector<int> ive2(ive);         //使用ive初始化ive2

向vectord元素Q?br>   while(cin>>word)
    {
        text.push_back(word);  //使用push_back可以向vector的后面插入新的元素?br>    }
注意Q?vector对象Q以及其他标准库容器对象Q的重要属性就在于可以在运行时高效的添加元素,因ؓvector增长效率高,在元素值已知的情况下,最好是动态的d元素?br>         *虽然可以对给定元素个数的vector对象预先分配内存Q但更有效的Ҏ是初始化一个空vector对象Q然后再动态的增加元素?br>
vector的下标操作:
for(vector<string>::size_type ix=0;ix!=text.size();++ix)
    {
        cout<<text[ix]<<endl;
    }
*使用vector<string>::size_typecd可以接收vector<string>cd的大?br>*不在for循环之前取得vector的大,而在for循环中计text.size()Q是因ؓvector是可以增长的Q所以它的元素数目是不确定的?br>*和string对象一Pvector可以通过下标直接讉K元素?br>
附表Qvector操作
v.empty() 如果v为空Q则q回true
v.size() q回v中元素的个数
v.push_back(t) 需按不qv的末֢加一个gؓt的元?/td>
v[n] q回v中位|ؓn的元?/td>
v1=v2 吧v1的g替换为v2中元素的副本
v1==v2 如果v1与v2相等Q则q回true
!=,<,<=,>,>= 保持q些操作W惯有的含义


]]>
c++字符处理 cctypehttp://www.shnenglu.com/liujiajia/archive/2008/06/01/51853.html刘加?/dc:creator>刘加?/author>Sun, 01 Jun 2008 15:45:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/06/01/51853.htmlhttp://www.shnenglu.com/liujiajia/comments/51853.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/06/01/51853.html#Feedback2http://www.shnenglu.com/liujiajia/comments/commentRss/51853.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/51853.html
isalnum(c) 如果c是字母或数字Q则为true
isalpah(c) 如果c是字母,则ؓtrue
iscntrl(c) 如果c是控制字W,则ؓtrue
isdigit(c) 如果c是数字,则ؓtrue
isgraph(c) 如果c不是I格Q则为true
islower(c) 如果c是小写字母,则ؓtrue
isprint(c) 如果c是可打印的字W,则ؓtrue
ispunct(c) 如果c是标点符P则ؓtrue
isspace(c) 如果c是空白字W,则ؓtrue
isupper(c) 如果c是大些字母,则ؓtrue
isxdigit(c) 如果c是十六进制数Q则为true
tolower(c) 如果c是大写字母,则返回小写字母Ş式,否则q回c?/td>
toupper(c) 如果c是小写字母,则返回大些字母Ş式,否则q回c?/td>


]]>
string对象的操?size() empty() 关系q算 赋?下标讉Khttp://www.shnenglu.com/liujiajia/archive/2008/06/01/51849.html刘加?/dc:creator>刘加?/author>Sun, 01 Jun 2008 15:22:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/06/01/51849.htmlhttp://www.shnenglu.com/liujiajia/comments/51849.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/06/01/51849.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/51849.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/51849.htmlstring对象的操作:

size():用来获取字符串的长度?br>输出字符串s的长度:
    string s("hello world!\n");
    cout<<"the size of '"<<s<<"' is"<<s.size()<<endl;

判断字符串是否ؓI的两种方式Q?br>    if(s.size()==0)
    {
        cout<<"s.size==0"<<endl;
    }
    if(s.empty())
    {
        cout<<"s.empty()"<<endl;
    }

获得字符串长度:
    int num=s.size();    使用整Ş接收字符串长度可能会有问题,因ؓint变量的表C围太,有时不能存储string对象的长度。用string::size_typecd可以满要求Q这U类型是unsigned的,q比signedcd要大一倍。事实上size()函数q回的也?size_type cd?br>
关系q算Q?br>可以使用Qs1==s2Qs1>s2Qs1<s2Q来比较两个字符丌Ӏ?br>
赋D:
    string snew="empty";
    snew=s1;
上面代码首先初始化一个snew字符ԌW二句将snew占用的内存释放掉Q然后给snew_存放s1副本的内存空_最后把s1中所有的字符复制到新分配的内存空间?br>
字符串连接:
    cout<<s1+"."<<endl;    OK
    cout<<"1"+s1<<endl;   OK
    cout<<s1+s2<<endl;    OK
    cout<<s1+s2+"."<<endl; OK
    //cout<<"hello"+"jiajia"<<endl;  ERROR
当进行string对象和字W串字面值؜合连接操作时Q?操作W的左右操作数必至有一个是stringcd的?br>
通过下标讉Kstring对象Q?/strong>
    string对象下标?开始?br>    for(string::size_type ix=0;ix<s1.size();ix++)
    {
        s1[ix]+=ix;
    }

刘加?/a> 2008-06-01 23:22 发表评论
]]>
字符串的d cin ?getline()http://www.shnenglu.com/liujiajia/archive/2008/06/01/51716.html刘加?/dc:creator>刘加?/author>Sat, 31 May 2008 16:02:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/06/01/51716.htmlhttp://www.shnenglu.com/liujiajia/comments/51716.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/06/01/51716.html#Feedback1http://www.shnenglu.com/liujiajia/comments/commentRss/51716.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/51716.htmlcin
    string s;
    cout<<"Enter some words...";
    cin>>s;
    cout<<"You write:"<<s<<endl;
*dq忽略开头所有的I白字符Q空根{换行符、制表符Q?br>*d字符直至遇到I白字符Q读取终止?br>
getline()
    string line;
    cout<<"Enter a line...";
    getline(cin,line);
    cout<<"You write:"<<line<<endl;
*q个函数接收两个参数Q第一个是输入对象,W二个是string对象?br>*getline不忽略行开头的换行W,只要getline()遇到换行W,哪怕是W一个字W,getline也将停止d字符q返回?br>*能够d除换行之外的I白字符?br>


]]>
c++初始化字W串的方?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/05/31/51714.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/51714.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/05/31/51714.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/51714.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liujiajia/services/trackbacks/51714.html</trackback:ping><description><![CDATA[C++初始化字W串的方式很灉|Q?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"><</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>    </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> s1;              </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">初始化ؓI字W串 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> s2(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">hello</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);     </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">初始化ؓ一个字W串字面副本 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> s3(s2);          </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">s3初始化ؓ一个s2的副?nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> s4</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">hi jiajia</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;  </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">初始化ؓ一个字W串字面副本 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> 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>    cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">s1</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">s2</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">s3</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">s4</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">s5</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br>    system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}</span></div> <br>注意Q由于历史原因以及ؓ了与C语言兼容Q字W串字面g标准库stringcd不是同一U类型? <img src ="http://www.shnenglu.com/liujiajia/aggbug/51714.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-05-31 23:48 <a href="http://www.shnenglu.com/liujiajia/archive/2008/05/31/51714.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>命名I间的using声明http://www.shnenglu.com/liujiajia/archive/2008/05/31/51713.html刘加?/dc:creator>刘加?/author>Sat, 31 May 2008 15:36:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/05/31/51713.htmlhttp://www.shnenglu.com/liujiajia/comments/51713.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/05/31/51713.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/51713.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/51713.html : : 操作W是作用域解析符。它的含义是x作数的名字可以在左操作数的作用域中找到。std::cout 的意思是说所需名字cin是在命名I间std中定义的。这U符号引用标准库名字的方式是非常ȝ的?br>     c++提供了一U简单的方式来用命名空间成员,using声明。这是一U最安全的机制?br> 
#include<string>
#include
<iostream>
using std::cin;
using std::endl;
using std::string;
int main(void)
{
    
string s;  //正确。string使用了using声明
    cin>>s;    //正确。cin使用了using声明
    cout<<s;   //错误。cout没有使用using声明
    std::cout<<s<<endl; //正确。指定了命名I间
    system("pause");    
    
return 0;
}


命名I间扩展Q?/strong>
        在标准C++以前Q都是用#include<iostream.h>q样的写法的Q因包含q来的头文g名就是iostream.h。标准C++引入了名字空间的概念Qƈ把iostream{标准库中的东东装Cstd名字I间中,同时Z不与原来的头文ghQ规定标准C++使用一套新的头文gQ这套头文g的文件名后不?h扩展名,如iostream、string{等Qƈ且把原来C标准库的头文件也重新命名Q如原来的string.h改成cstring(是?hLQ前面加上字母c)Q所以头文g包含的写法也变成了#include <iostream>?
q不是写?include<iostream>必ȝusing namespace std;我们通常q样的写的原因是Z一下子把std名字I间的东东全部暴露到全局域中Q就像是直接包含了iostream.hq种没有名字I间的头文g一PQ标准C++库用h与传l的iostream.h一hѝ如果不用using namespace std;使用标准库时得时时带上名字I间的全名,如std::cout << "hello" << std::endl;
     #include "iostream"?include<iostream>的区别:前者先在当前目录找iostream文gQ找不到再去pȝ头文件\径找Q后者反之。因此,做ؓ一个良好的习惯Q在包含pȝ头文件时量?lt;>Q而在包含自己的工E中的头文g时用""?

]]>
开始学习c++语言http://www.shnenglu.com/liujiajia/archive/2008/05/31/51710.html刘加?/dc:creator>刘加?/author>Sat, 31 May 2008 15:07:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/05/31/51710.htmlhttp://www.shnenglu.com/liujiajia/comments/51710.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/05/31/51710.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/51710.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/51710.html本来学习c是Z学c++而准备的Q但是感觉进度有Ҏ了,看了些基的,准备学c++了?br>
对c++语言的学习,我将用那本经典的《C++ Primer?中文?。另外编辑器使用Dev-c++4.9.9.2?

另外Q由于对C语言的学习和以前C++的一点基Q所以选择了《C++ Primer》这本书。但是,如果没有q方面的基础Q徏议看《C++ Primer Plus》这本书?

]]>
sprint() 格式化写入字W串http://www.shnenglu.com/liujiajia/archive/2008/05/31/51655.html刘加?/dc:creator>刘加?/author>Fri, 30 May 2008 16:01:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/05/31/51655.htmlhttp://www.shnenglu.com/liujiajia/comments/51655.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/05/31/51655.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/51655.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/51655.htmlE序从读取用戯入的firstname,lastname和prize,q将他们d的值按照一定格式存储到另一个字W串数组内?/p>
#include<stdio.h>
#define MAX 20
int main(void)
{
    
char first[MAX];
    
char last[MAX];
    
char formal[2*MAX+10];
    
double prize;
    
    puts(
"Enter your first name");
    gets(first);
    puts(
"Enter your last name");
    gets(last);
    puts(
"Enter your prize money");
    scanf(
"%lf",&prize);
    sprintf(formal,
"%s,%-19s:$%6.2f\n",last,first,prize);
    puts(formal);
    
    getchar();
    getchar();
    
return 0;
}


]]>
strcpy() 复制字符?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/05/30/51654.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/51654.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/05/30/51654.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/51654.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liujiajia/services/trackbacks/51654.html</trackback:ping><description><![CDATA[下面代码演示了程序@环从用户输入d字符Ԍq存入时的字符串数l,然后时字W串复制到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"><</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">.h</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> SIZE 40</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> LIM 5</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> qwords[LIM][SIZE];<br>    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> temp[SIZE];<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>    <br>    </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">LIM </span><span style="COLOR: #000000">&&</span><span style="COLOR: #000000"> gets(temp))<br>    {<br>        <strong>strcpy</strong>(qwords[i],temp);<br>        i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>    }<br>    puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">The list</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>    </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"><</span><span style="COLOR: #000000">LIM;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>    {<br>        puts(qwords[i]);<br>    }<br>    <br>    getchar();<br>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}</span></div> <img src ="http://www.shnenglu.com/liujiajia/aggbug/51654.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-05-30 23:57 <a href="http://www.shnenglu.com/liujiajia/archive/2008/05/30/51654.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>strcmp() 比较字符串的内容http://www.shnenglu.com/liujiajia/archive/2008/05/30/51653.html刘加?/dc:creator>刘加?/author>Fri, 30 May 2008 15:48:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/05/30/51653.htmlhttp://www.shnenglu.com/liujiajia/comments/51653.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/05/30/51653.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/51653.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/51653.html
    char try[10];
    
    puts(
"Who is 1+1?");
    gets(
try);
    
while(strcmp(try,"2")!=0)
    {
        puts(
"No,it's wrong.Try again.");
        gets(
try);
    }
    puts(
"OK,That's right");


下面E序说明各种情况下,strcmp()的返回|

printf("strcmp(\"a\",\"a\")=%d\n",strcmp("a","a"));
    printf(
"strcmp(\"a\",\"b\")=%d\n",strcmp("a","b"));
    printf(
"strcmp(\"b\",\"a\")=%d\n",strcmp("b","a"));
    printf(
"strcmp(\"a\",\"c\")=%d\n",strcmp("a","c"));
    printf(
"strcmp(\"c\",\"a\")=%d\n",strcmp("c","a"));
    printf(
"strcmp(\"abc\",\"abd\")=%d\n",strcmp("abc","abd"));
    printf(
"strcmp(\"abc\",\"abcd\")=%d\n",strcmp("abc","abcd"));
    printf(
"strcmp(\"abc\",\"abbd\")=%d\n",strcmp("abc","abbd"));



]]>
strcat()函数http://www.shnenglu.com/liujiajia/archive/2008/05/27/51323.html刘加?/dc:creator>刘加?/author>Tue, 27 May 2008 12:12:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/05/27/51323.htmlhttp://www.shnenglu.com/liujiajia/comments/51323.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/05/27/51323.html#Feedback1http://www.shnenglu.com/liujiajia/comments/commentRss/51323.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/51323.html
#include<stdio.h>
#include
<string.h>
int main(void)
{
    
char string1[20];
    
char string2[]="goodbye";
    
    gets(string1);
    strcat(string1,string2);
    puts(string2);
    puts(string1);
    
    getchar();
    
return 0;
}
以上E序string2字符串的拯攑ֈ了string1的结?br>
值得注意的是Qstring1在被使用strcat()函数之前Q应该是已经初始化或者已l存储了一个字W串。因为如果字W串没有被初始化Qstring1[]里面元素的存储是一串随机倹{strcat()需要找到string1的结(W一个出现的'\0'Q?然后Lq个'0',最后把string2[]的拷贝放到string1[]最后一个值的后面?

]]>
使用strlen()获得字符串长?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/05/27/51321.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/51321.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/05/27/51321.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/51321.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liujiajia/services/trackbacks/51321.html</trackback:ping><description><![CDATA[函数原型Qsize_t strlen(const char*)<br>作用Q返回参C字符串的长度?br><br>以下代码是一个截取字W串的例子:<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"><</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">.h</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> fit(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> </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"> main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> msg[]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello moto</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>    <br>    puts(msg);<br>    fit(msg,</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">);<br>    puts(msg);<br>    puts(msg</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">);<br>    getchar();<br>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> fit(</span><span style="COLOR: #0000ff">char</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: #0000ff">int</span><span style="COLOR: #000000"> size)<br>{<br>    </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">></span><span style="COLOR: #000000">size)<br>    {<br>        </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>    }<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       </td> <td>m</td> <td>o</td> <td>t</td> <td>o</td> <td>\0</td> </tr> </tbody> </table> <br>fit()是截取字W串的方法。如果字W串的长度大于要截取的长度,那么指向该字符串的指针向后Udsize位后Q将指针指向的内容置?\0'Q即字符串结束。但string+size+1处仍然存在一个字W串? <img src ="http://www.shnenglu.com/liujiajia/aggbug/51321.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-05-27 19:32 <a href="http://www.shnenglu.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()Q?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/05/27/51304.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/51304.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/05/27/51304.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/51304.html</wfw:commentRss><trackback:ping>http://www.shnenglu.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>    char name[20];<br>    printf("Hi,What's your name?\n");<br>    gets(name);<br>    printf("Nice name %s",name);<br>它用一个地址把字W串赋予name?br><br>    char name[20];<br>    char *p;<br>    printf("\nEn,What's your name?\n");<br>    p=gets(name);<br>    printf("%s?Oh,Nice name %s\n",name,p);<br>get()的代码用return 关键字返回字W串的地址Q程序把q个地址分配l指针p?br><br><em><strong>fgets()</strong></em><br>fgets()是ؓ文gI/O而设计的Q处理键盘输入不是特别方ѝ?br><br>    printf("\nHi,What's your name?\n");<br>    p=fgets(name,20,stdin);<br>    printf("%s?Oh,Nice name %s\n",name,p);<br>*fgets()的第二个参数说明最大读入的字符数。如果这个参数gؓnQ那么fgets()׃d最多n-1个字W或d一个换行符为止。两个条件满Q意一个结束?br>*fgets()d到换行符Q就会把它存到字W串里,而不是想gets()那样丢弃它?br>*fgets()的第三个参数说明d个文件。从键盘上读数据Ӟ可以使用stdin(代表standard input)作ؓ参数?br><br><strong><em>scanf()</em></strong><br>    char name1[11], name2[11];<br>    int count;<br>    printf("\nPlease write down 2 names...\n");<br>    count=scanf("%5s %6s",name1,name2);<br>    printf("\nname1:%s\nname2:%s",name1,name2);<br><br>scanf()允许指定输入字符串长度等格式。上面的E序如果输入"liujiajia liujiajia",E序输?name1:liuji   name2:liujia";<br><br><span style="FONT-SIZE: 18pt"><strong>字符串输出:</strong></span><br><em><strong>puts()</strong></em><br>    char str[15]="hello world";<br>    const char *str2="HELLO WORLD";<br>    <br>    puts(str);<br>    puts(str2);<br>    puts(&str[5]);<br>    puts(str2+2);<br>puts()昄字符串时自动在其后添加一个换行符?br>puts(&str[5]);输Zstr的第六个元素开始到字符串结束?br>puts(str2+2);输Zstr2的地址向后Ud两个字符开始到字符串结束?br><br><strong><em>fputs()</em></strong><br>    fputs(str,stdout);<br>    fputs(str2,stdout);<br>    fputs(&str[5],stdout);<br>    fputs(str2+2,stdout);<br>*fputs()W二个参数表C写的文g。可以用stdout(代表standard output)作ؓ参数?br>*fputs()不自动输出换行符Q这与puts()不太相同?br><br><strong><em>printf()</em></strong><br>q个不用多说了? <img src ="http://www.shnenglu.com/liujiajia/aggbug/51304.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-05-27 18:03 <a href="http://www.shnenglu.com/liujiajia/archive/2008/05/27/51304.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>初步认识数组、指针和字符串关p?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/05/24/50913.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/50913.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/05/24/50913.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/50913.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liujiajia/services/trackbacks/50913.html</trackback:ping><description><![CDATA[<p><strong><span style="FONT-SIZE: 18pt">字符串数l初始化Q?/span><br>(1)char arr[10]="HI";</strong><br>初始化字W串数组Ӟ~译器自动将字符串最后一个字W后面加?\0',以表C字W串的结束?br>如果数组的大大于字W串的长?1Q那么把字符串结束后面的元素也都初始化ؓ'\0'; <br>看这D늨序:<br>定义一个大ؓ12的字W串数组<br>char str1[12]="jiajia";        <br>让一个char型的指针指向q个数组<br>    char *p1=str1;<br>输出q个字符?br>    printf("%s\n\n",str1);<br>利用指针输出q个字符串数l中每个元素的字W、字Wascii倹{字W存储地址<br>    for(int i=0;i<12;i++)<br>    {<br>        printf("%c:%d:%p\n",*p1,*p1,p1);<br>        p1++;<br>    }<br>可以看出Q数l元素的后几位都被初始化成了'\0';使用printf打印字符串数l时Q程序遇到第一?\0'q束对字符串的d?br><br>如果数组的大小于字W串的长?1Q程序在q行时可能要出现问题Q所以应当确保数l的大小要大于字W串长度的大?1Q?br><br>q段E序的字W串长度+1于数组的大:<br>char str2[12]="hello jiajia";<br>    char *p2=str2;</p> <p>    printf("\n\n\n%s\n\n",str2);<br>    <br>    for(int i=0;i<28;i++)<br>    {<br>        printf("%c:%d:%p\n",*p2,*p2,p2);<br>        p2++;<br>    }<br><br>使用printf打印字符ԌE序需要找?\0'才能l束dQ结果找遍整个数l没有找刎ͼ只有l箋着地址LQ一直找到某个地址上的assii?的,字符串才d完毕。所以本D늨序在d?hello jiajia"之后Q后面可能还会有几个随机的字W,是因ؓ字符串终止于'\0'?br><br><strong>(2)char arr[]="Hello World!";</strong><br>q种初始化方法不用担心数l大过的情况Q数l的大小q译器军_?br><strong>(3)char arr[]={'H','e','e','l','o',' ','W','o','r','l','d','\0'};</strong><br>同第二种Ҏl果相同Q但昄要麻烦许多。另外,' '代表I格Qascii值是32Q?\0'代表字符串结束,ascii值是0Q?br><strong>(4)char *p="Hello World";</strong><br>使用数组的方式初始化。同数组不同之处在于,p是一个变量,可以做递增、递减q算Q而arr是数l的首地址Q是一个地址帔RQ不能做递增、递减q算?br><br>char *name="liujiajia";<br>    name[0]='L';<br>    printf("\n%s\n",name);<br>name[0]='L';q句代码在最新的c99标准中会引发E序异常。需要注意?br><br><br>char *string="c programe";<br>    char *string2;    <br>    string2=string;    //指针string的Dlstring2<br>    <br>    printf("\nstring=%s,&string=%p,string=%p\n",string,&string,string);<br>    printf("\nstring=%s,&string=%p,string=%p\n",string2,&string2,string2);<br><br>上面q段E序最后显C,指针string所指向的地址和指针string2所指向的地址相同Q这p明它们指向的是内存里面同一个字W串Q也是说字W串本nq没有复Ӟ而是产生了一个指向同一个字W串的指针。这L序的效率会更加的高。如果需要复制字W串可以使用 strcpy() ?strncpy()?br><br></p> <img src ="http://www.shnenglu.com/liujiajia/aggbug/50913.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-05-24 00:27 <a href="http://www.shnenglu.com/liujiajia/archive/2008/05/24/50913.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>函数和二l数l的新认?参数指定数组W二l和W一l大?http://www.shnenglu.com/liujiajia/archive/2008/05/20/50581.html刘加?/dc:creator>刘加?/author>Tue, 20 May 2008 14:31:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/05/20/50581.htmlhttp://www.shnenglu.com/liujiajia/comments/50581.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/05/20/50581.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/50581.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/50581.html
#include<stdio.h>
#define ROWS 3
#define COLS 4
void sum_rows(int arr[][COLS],int rows);
int main(void)
{
    
int junk[ROWS][COLS]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
    
    sum_rows(junk,ROWS);
//求出每行的数值d?nbsp;
    
    getchar();
    
return 0;

void sum_rows(int arr[][COLS],int rows)
{
    
int r,c,tot;
    
for(r=0;r<rows;r++)
    {
        tot
=0;
        
for(c=0;c<COLS;c++)
        {
            tot
+=arr[r][c];
        }
        printf(
"row%d,sum=%d\n",r,tot);
    }
}

我们都知道上面的代码可以利执行Q?sum_rows()的Ş参是 int arr[][COLS],没有指定数组的第一l大。但是设想一下,如果不指定第二维的大,l果会怎样呢?
我写了以下例子:
#include <stdio.h> 
int sum2d(int arr[][],int row,int col); 
int main(void

    
int arr[3][4]={{1,2,3,5},{3,4,4,5},{2,3,4,5}}; 
    sum2d(arr,3
,4); 
     
    getchar(); 
    
return 0;  

int sum2d(int arr[][],int row,int col) 

    
for(int i=0;i <row;i++
    { 
        
for(int j=0;j <col;j++
        { 
            printf(
"%d",arr[i][j]);  invalid use of array with unspecified bounds         
        } 
    } 
    
return 0

q行后发玎ͼE序报错了,q是Z么呢Q百思不得其解,后来h了CSDN上老大们。终于稍微弄明白了点ѝ我惛_体应该从两方面证明ؓ什么上面的E序不可行?br>Q?Q从二维数组的存储方式上Q二l数l在内存上是一行一行连l的存储的。如下图Q?br>arr[3][4]
arr[0][0] arr[0][1] arr[0][2] arr[0][3] arr[1][0] arr[1][1] arr[1][2] arr[1][3] ...

因ؓarr是这个数l的首地址Q我们可以知道arr[0][0]的地址。只要我们知道arr[][4]中数l第二维的大?#8216;4’Q我们就可以扑ֈL行和列的地址。address=arr+(rows-1)*4+cols。但是如果不知道W二l的大小Q那么条件就不够了。打一个比方,若q个学生分成每组11人,q让他们站成一排,q时我们可以定位出第几组的第几个学生。但如果不知道每l?1个hQ那么我们显然就无法定位了?br>
Q?Q从指针的角度上?q种思\我还是有点迷p?:大家可以参看q个帖子Q?a >http://topic.csdn.net/u/20080520/09/2122e210-5cf5-4b75-b31f-c2523b00cf53.html?575319732

恩,现在是越学越qh了。呵c?br>

]]>
函数和二l数l?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/05/19/50446.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/50446.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/05/19/50446.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/50446.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liujiajia/services/trackbacks/50446.html</trackback:ping><description><![CDATA[阅读下面代码Q程序列举了3U二l数l作为参C递的ҎQ?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"><</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> ROWS 3</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> COLS 4</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> sum_rows(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> arr[][COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> rows);<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> sum_cols(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> [][COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">);        </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"> sum2d(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">arr)[COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> rows);   </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">另一U语法Ş?/span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> 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>    <br>    sum_rows(junk,ROWS);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求出每行的数值d?nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);       </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求出每列的数值d?nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    sum_cols(junk,ROWS);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求出全部数值的d?nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    <br>    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\nthe sum of the junk is %d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,sum2d(junk,ROWS));<br>    <br>    getchar();<br>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>} <br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> sum_rows(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> arr[][COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> rows)<br>{<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> r,c,tot;<br>    </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"><</span><span style="COLOR: #000000">rows;r</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>    {<br>        tot</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>        </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"><</span><span style="COLOR: #000000">COLS;c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>        {<br>            tot</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">arr[r][c];<br>        }<br>        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>    }<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> sum_cols(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> arr[][COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> rows)<br>{<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> r,c,tot;<br>    </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"><</span><span style="COLOR: #000000">COLS;c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>    {<br>        tot</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>        </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"><</span><span style="COLOR: #000000">rows;r</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>        {<br>            tot</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">arr[r][c];<br>        }<br>        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>    }<br>}<br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> sum2d(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">arr)[COLS],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> rows)<br>{<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> r,c,tot</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>    </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"><</span><span style="COLOR: #000000">rows;r</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>    {<br>        </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"><</span><span style="COLOR: #000000">COLS;c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>        {<br>            tot</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">arr[r][c];<br>        }<br>    }<br>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> tot;<br>}<br></span></div> <img src ="http://www.shnenglu.com/liujiajia/aggbug/50446.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-05-19 23:03 <a href="http://www.shnenglu.com/liujiajia/archive/2008/05/19/50446.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>指针和多l数l?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/05/19/50440.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/50440.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/05/19/50440.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/50440.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liujiajia/services/trackbacks/50440.html</trackback:ping><description><![CDATA[<strong>一、多l数l地址和指针的关系</strong><br>int arr2[4][2]={{1,2},{3,4},{5,6},{7,8}};<br>printf("arr2=%p,&arr2[0]=%p,arr2[0]=%p,&ar2r[0][0]=%p\n",arr2,&arr2[0],arr2[0],&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>上面的程序的l果输出竟然都一栗ؓ什么呢Q?br>首先 arr2 代表的是arr2数组的首地址Q所?arr2的值和 arr2W一个子元素arr2[0]的地址相同Q即 &arr2[0]Q?br>arr2[0]是含有两个整形元素一l数l的首地址Q即&arr2[0][0]的倹{?br>单的_arr[0]是一个整数大对象的地址Qarr2是两个整数大对象的地址。因为整数和两个整数l成的数l开始于同一个地址Q所?arr2 ?arr2[0] h相同的数倹{?br><br><strong>二、指针(地址Q运和多维数组的关p?/strong><br>printf("arr2+1=%p,arr2[0]+1=%p\n",arr2+1,arr2[0]+1);<br>现在我们知道Qarr2 ?arr2[0]h相同的数|但是arr2+1和arr2[0]+1Z么数g一样呢Q这是因为,arr2指向的对象大是两个intQ儿arr2[0]所指向的大是一个int。所以arr2+1的D比arr2[0]+1的值多一个int大小?br><br><strong>三、?对多l数l取?/strong><br>因ؓ arr2[0] ?arr2[0][0]的首地址Q所?*(arr2[0])代表存储在arr2[0][0]的倹{同Parr2是arr2[0]的首地址Q?arr是 arr2[0]的倹{但?arr2[0]也是一个地址Q即&arr2[0][0],因此*arr2?&arr2[0][0]。所?*&arr2[0][0] {h?**arr2?&arr2[0][0] 可以化ؓ arr2[0][0]。所?arr2[0][0]==**arr2?br>而言之:arr2是地址的地址Q所以需要经q两ơ取值才能取到具体的数倹{?br><br><br>学完q个知识点,才发现在指针实很难懂?br> <img src ="http://www.shnenglu.com/liujiajia/aggbug/50440.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-05-19 22:28 <a href="http://www.shnenglu.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.shnenglu.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.shnenglu.com/liujiajia/archive/2008/05/19/50421.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/50421.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/05/19/50421.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/50421.html</wfw:commentRss><trackback:ping>http://www.shnenglu.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>的操作。请参看以下代码展示了几U情形:<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"><</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> 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>    </span><span style="COLOR: #0000ff">int</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">p2,</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p3;<br>    <br>    p1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">urn;         </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">把一个地址赋给指针 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    p2</span><span style="COLOR: #000000">=&</span><span style="COLOR: #000000">urn[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">];     </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">取得urn[2]的地址Qƈ赋给一个指?/span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    <br>    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">数组各个元素的值和地址Q\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); <br>    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</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"><</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>    {<br>        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">urn[%d]=%d   &urn[%d]=%p;\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,i,urn[i],i,</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">urn[i]);<br>    }<br>    <br>    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n指针指向的地址,指针指向地址的?指针的地址\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">p1=%p    ,*p1=%d         ,&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">&</span><span style="COLOR: #000000">p1); <br><br>    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n指针加上整数的效?指针+int!=指针Q指?int==地址)Q\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>    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>    <br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">printf("&(p1+4)=%p",&(p1+4))</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">error:p1+4是一个地址Q不能对其进行取地址操作Q但可以p1+4赋给一个指?nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    <br>    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是一个地址Q将p1+1赋给p1</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n在p1++以后Q\np1=%p    ,*p1=%d         ,&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">&</span><span style="COLOR: #000000">p1);<br>    <br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求两个指针差?nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    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); <br>    <br>    getchar();<br>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}</span></div> <img src ="http://www.shnenglu.com/liujiajia/aggbug/50421.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-05-19 19:08 <a href="http://www.shnenglu.com/liujiajia/archive/2008/05/19/50421.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>带有数组cd参数的函?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/05/16/50110.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/50110.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/05/16/50110.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/50110.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liujiajia/services/trackbacks/50110.html</trackback:ping><description><![CDATA[<p>传递数l参敎ͼ其核心就是传递该数组的首地址。然后函数再通过数组、指针等Q用该数l的首地址构造一个新的数l或指针Q再通过传递过来的数组大小Q对该数l进行操作?/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"><</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> SIZE 4</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> sumbyarr(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> a[],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> n);<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> sumbypointer(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> n);<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> sumbyaddress(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> address,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> n);<br>int sumbypointer2(int *begin,int *end)<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> 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>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> sum1;<br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">使用数组作ؓ形参接收数组 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    sum1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sumbyarr(arr,SIZE);<br>    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">the total of the arr by array is:%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,sum1);<br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">使用指针作ؓ形参接收数组 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> sum2;<br>    sum2</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sumbypointer(arr,SIZE);<br>    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">the total of the arr by pointer is:%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,sum2);<br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">使用地址作ؓ形参接收数组 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> sum3;<br>    sum3</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sumbyaddress(arr,SIZE);<br>    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">the total of the arr by address is:%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,sum3);<br>       <br>    //使用两个指针形参接收<br>    int sum4;<br>    sum4=sumbypointer2(arr,arr+SIZE);<br>    printf(<span style="COLOR: #000000">"</span><span style="COLOR: #000000">the total of the arr by pointer2 is:%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,sum4);<br></span><br><br>    getchar();<br>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>} <br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> sumbyarr(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> a[],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> n)<br>{ <br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> index;<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> total</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>    </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"><</span><span style="COLOR: #000000">n;index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>    {<br>        total</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">a[index];<br>    }<br>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> total;<br>}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> sumbypointer(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> n)<br>{<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> index;<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> total</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>    </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"><</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>    {<br>        total</span><span style="COLOR: #000000">+=*</span><span style="COLOR: #000000">p;<br>    }<br>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> total;<br>}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> sumbyaddress(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> address,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> n)<br>{<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">address;<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> index;<br>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> total</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>    </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"><</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>    {<br>        total</span><span style="COLOR: #000000">+=*</span><span style="COLOR: #000000">p;<br>    }<br>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> total;<br>}<br>int sumbypointer2(int *begin,int *end)<br>{<br>    int total=0;<br>    while(begin<end)<br>    {<br>        total+=*begin;<br>        begin++;<br>    }<br>    return total;<br>}</span></div> <br>以上代码演示怎样定义有数l作为参数的函数?br>如果数组作ؓ参数传递进入某个函敎ͼq且在这个函C改变了数l元素的|那么E序q回后,q个数组元素的值有没有改变呢?<br><br>当然Q值改变了Q因为数l是通过地址传递的Q改变了被调函数中数l的g意味着同时改变了主调函C数组的倹{因为它们的地址是相同的? <img src ="http://www.shnenglu.com/liujiajia/aggbug/50110.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-05-16 23:54 <a href="http://www.shnenglu.com/liujiajia/archive/2008/05/16/50110.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>认识数组和指针的关系http://www.shnenglu.com/liujiajia/archive/2008/05/16/50097.html刘加?/dc:creator>刘加?/author>Fri, 16 May 2008 13:30:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/05/16/50097.htmlhttp://www.shnenglu.com/liujiajia/comments/50097.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/05/16/50097.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/50097.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/50097.html    int arr[SIZE]={1,2,3,4};
    int index;
    for(index=0;index<SIZE;index++)
    {
        printf("arr[%d]=%d address=%p\n",index,arr[index],arr+index);
    }
   
    int *p=arr;
    for(index=0;index<SIZE;index++)
    {
        printf("*(p+%d)=%d     p+%d=%p\n",index,*(p+index),index,p+index);
    }
   
    getchar();
    return 0;

以上代码展示了读取数l中元素的两U方式,W一U是常规的用数l下标来d。第二种Ҏ是用指针来d数组?br>
通过W一中方法中打印出来的内容,我们可以认识刎ͼ数组名其实就是这个数l的首地址Q即Qarr=数组首地址;arr+1:数组W二个元素的地址Qarr+2Q数l第三个元素的地址Q以此类推。。?br>
W二U方法我们用指针访问代替了数组下标讉KQ我们将 arr(xl的首地址)赋值给了指?*pQ所?pq于arr的首地址Qp+1q于arr的二个元素的地址Q那?pq当于了arr[0],*(p+1)q当于arr[1]?br>
我们从中可以得出一个结论:数组标记实际上是一U变怋用指针的形式?

]]>
二维数组单?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/05/16/49998.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/49998.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/05/16/49998.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/49998.html</wfw:commentRss><trackback:ping>http://www.shnenglu.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"><</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> PLAYER 4</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> MATCH 3</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">?名球员近三场比赛的得分初始化</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> arrmark[MATCH][PLAYER]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{<br>        {</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>        {</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>        {</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>        }; <br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">输出具体成W</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i,j;<br>    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">***********Roony  Ronaldo  Kaka  Grosso\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>    </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"><</span><span style="COLOR: #000000">MATCH;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>    {<br>        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>        </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"><</span><span style="COLOR: #000000">PLAYER;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>        {<br>            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>        }<br>        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>    }<br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">打印d</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> temp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">total        </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>    </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"><</span><span style="COLOR: #000000">PLAYER;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)   </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">求每个球员的d </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    {<br>        </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"><</span><span style="COLOR: #000000">MATCH;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>        {<br>            temp</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">arrmark[j][i];<br>        }<br>        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d      </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,temp);<br>        temp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>    } <br>    getchar();<br>} </span></div> <img src ="http://www.shnenglu.com/liujiajia/aggbug/49998.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-05-16 00:06 <a href="http://www.shnenglu.com/liujiajia/archive/2008/05/16/49998.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>指定数组大小http://www.shnenglu.com/liujiajia/archive/2008/05/15/49996.html刘加?/dc:creator>刘加?/author>Thu, 15 May 2008 15:32:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/05/15/49996.htmlhttp://www.shnenglu.com/liujiajia/comments/49996.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/05/15/49996.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/49996.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/49996.html    char arr[10];                                     //OK
    char arr2[1+2*7];                            //OK
    char arr3[sizeof(char)+2];                //OK
    char arr4[-2];                                  //error 数组大小不能使用负数
    char arr5[0];                                   //Ҏ~译器不同,有的~译器{换ؓ“arr5[]”,有的~译不通过?/span>
    char arr6[3.14];                              //error
    char arr7[(int)3.14];                       //强制转换为int
    int m=2;
    char arr9[m];                                //c99前不允许

注意Qc99标准允许使用变量指定数组大小?

]]>
认识数组之数l的初始?/title><link>http://www.shnenglu.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.shnenglu.com/liujiajia/archive/2008/05/15/49994.html</guid><wfw:comment>http://www.shnenglu.com/liujiajia/comments/49994.html</wfw:comment><comments>http://www.shnenglu.com/liujiajia/archive/2008/05/15/49994.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liujiajia/comments/commentRss/49994.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liujiajia/services/trackbacks/49994.html</trackback:ping><description><![CDATA[以下代码展示数组初始化的各种情况Q?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"><</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> SIZE 4</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br>{<br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">一般初始化 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> 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>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> index;<br>    </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"><</span><span style="COLOR: #000000">SIZE;index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>    {<br>        arr[index]</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>        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>    }<br>    <br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">定义q初始化一个const数组。数l内元素不能修改 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> 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>    <br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">数组不初始化Q元素的gؓ随机数?nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> arr3[SIZE];  <br>    </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"><</span><span style="COLOR: #000000">SIZE;index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>    {<br>        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>    }<br><br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">如果初始化数l中部分元素Q其他元素则被初始化? </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> 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">}; <br>    </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"><</span><span style="COLOR: #000000">SIZE;index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>    {<br>        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>    }<br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">指定要初始化的元?/span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> 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>    </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"><</span><span style="COLOR: #000000">SIZE;index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>    {<br>        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>    }<br>    <br>    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">数组的元素个C固定 </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> 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>    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">sizeof arr6=%d,sizeof 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"> arr6,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000"> arr6[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]);<br>    </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"><</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000"> arr6</span><span style="COLOR: #000000">/</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000"> arr6[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];index</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>    {<br>        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>    }<br>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>}</span></div> <br>E序q行l果是:<br><img height=426 alt="" src="http://www.shnenglu.com/images/cppblog_com/liujiajia/arrinit.jpg" width=284 border=0><br>怎么P数组的初始化方式多种多样。但要灵z运用? <img src ="http://www.shnenglu.com/liujiajia/aggbug/49994.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liujiajia/" target="_blank">刘加?/a> 2008-05-15 23:11 <a href="http://www.shnenglu.com/liujiajia/archive/2008/05/15/49994.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>认识sizeofhttp://www.shnenglu.com/liujiajia/archive/2008/05/15/49942.html刘加?/dc:creator>刘加?/author>Thu, 15 May 2008 08:01:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/05/15/49942.htmlhttp://www.shnenglu.com/liujiajia/comments/49942.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/05/15/49942.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/49942.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/49942.html以下代码演示sizeof用法Q?br>
#include<stdio.h>
#include
<stddef.h> 
#include
<stdlib.h>
int main(void)
{
    
int i;
    printf(
"sizeof(2008)=%d\n",sizeof(2008));
    printf(
"sizeof(i)=%d\n",sizeof(i));
    printf(
"sizeof i=%d\n",sizeof i);
    printf(
"sizeof(int)=%d\n",sizeof(int));
    
//printf("sizeof int=%d\n",sizeof int); //sizeof int写法错误Q?nbsp;
    printf("sizeof(char)=%d\n",sizeof(char));
    printf(
"sizeof(float)=%d\n",sizeof(float));
    printf(
"sizeof(double)=%d\n",sizeof(double));
    
    
return 0;
}

q行l果昄Q?br>

另外Qsizeof 可以应用于数l、结构等情况Q请参考这文章:http://blog.chinaunix.net/u/20828/showart_438003.html

]]>
使用igooglehttp://www.shnenglu.com/liujiajia/archive/2008/05/15/49882.html刘加?/dc:creator>刘加?/author>Wed, 14 May 2008 16:00:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/05/15/49882.htmlhttp://www.shnenglu.com/liujiajia/comments/49882.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/05/15/49882.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/49882.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/49882.htmligoogle 。可以自己添加googleZ提供的小工具来组成你的主c里面有个google眼睛Q感觉很好玩?br>可是我同时想把我一些常用的链接放进去,但是igoogleq没有提供相关的工具Q怎么办?没关p,googleZ提供了小工具开发的接口?br>
1.q入 igoogle ?br>2.点击“d内容”链接?br>3.点击“d供稿源或工?#8221;Q输?#8216;http://helloliukai.googlepages.com/hi.xml’ 点击d?br>4.然后回到首页Q你可以看到你的igoogle主页上添加了一个名?#8216;hello world example’的小工具?br>
呵呵Q你只要打开http://helloliukai.googlepages.com/hi.xml q个xml文g׃发现其中的问题了?br>igoogle通过解析xml文g来向用户展示工兗你只要?#8220;<![CDATA[Hello, world!]]>”中的“Hello, world!”替换成Q意html或javascript代码p了。然后把修改q的xml文g上传C个公qI间里面p了。我用的同样是google的品,Page CreatorQ?a >http://pages.google.com/ 。呵cgoogle的东东确实很多?br>另外Qxml实也很强大?br>
http://code.google.com/intl/zh-CN/apis/gadgets/ q里有google的资源,有教E和编辑器。但教程感觉已经q期了,好多里面的代码都不能用。可以用那个小~辑器打开各种模板?

]]>
认识递归函数http://www.shnenglu.com/liujiajia/archive/2008/05/14/49769.html刘加?/dc:creator>刘加?/author>Tue, 13 May 2008 16:08:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/05/14/49769.htmlhttp://www.shnenglu.com/liujiajia/comments/49769.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/05/14/49769.html#Feedback0http://www.shnenglu.com/liujiajia/comments/commentRss/49769.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/49769.html#include<stdio.h>
void testfunc(int);
int main(void)
{
    testfunc(
1);
    
return 0;
}
void testfunc(int n)
{
    printf(
"Lv%d:%p\n",n,&n); //%p 地址昄格式
    if(n<4)
    {
        testfunc(n
+1);  //testfunc()调用了自?/span>
    }
    printf(
"2Lv%d:%p\n",n,&n);
}
以上是一个递归的示例?br>testfunc()在内部调用了自己Q这L后果是:暂时不执行最后面?#8220;printf("2Lv%d:%p\n",n,&n);”Q而是?n+1作ؓ参数调用了自己,q样重复Q最l程序的l果是:

(Ҏ电脑不同Q地址可能有所不同)
E序具体q行步骤Q?br>1.main()调用testfunc(1)
2.testfunc(1)执行Q打?#8220;lv1:0022FF60”Q?022FF60是n的地址
3.n<4,条g成立Q?nbsp;testfunc(1)调用testfunc(2)
4.testfunc(2)执行Q打?#8220;lv1:0022FF40”,因ؓ此时testfunc(2)的变量n已经不是testfunc(1)中的变量n了,所以地址不同。(函数的参C用域只在当前函数Q?br>5.n<4Q条件成立,testfunc(2)调用testfunc(3)
6.testfunc(3)执行Q打?#8220;lv1:0022FF20”
7.n<4Q条件成立,testfunc(3)调用testfunc(4)
8.testfunc(4)执行Q打?#8220;lv1:0022FF00”
9.n<4,条g不成立,打印“2lv1:0022FF00”
10.q回调用testfunc(4)的testfunc(3),打印“2lv1:0022FF20”
11.q回调用testfunc(3)的testfunc(2),打印“2lv1:0022FF40”
12.q回调用testfunc(2)的testfunc(1),打印“2lv1:0022FF60”

OK,q就是函数的递归调用Q通俗的说是函数自己调用自己。以前学时没有弄明白Q现在终于弄明白了?br>

]]>
认识getchar()和putchar()http://www.shnenglu.com/liujiajia/archive/2008/05/09/49367.html刘加?/dc:creator>刘加?/author>Fri, 09 May 2008 14:55:00 GMThttp://www.shnenglu.com/liujiajia/archive/2008/05/09/49367.htmlhttp://www.shnenglu.com/liujiajia/comments/49367.htmlhttp://www.shnenglu.com/liujiajia/archive/2008/05/09/49367.html#Feedback2http://www.shnenglu.com/liujiajia/comments/commentRss/49367.htmlhttp://www.shnenglu.com/liujiajia/services/trackbacks/49367.htmlch=getchar(); ?scanf("%c",&ch);是同L效果?br>

putchar()函数用来打印它的参数?br>putchar(ch); ?printf("%c",ch); 是同L效果?br>
q两个函数都?stdio.h 文g中定义了?



]]>
99ξþþŷƷվ| 2021¾þþӾƷ| þӰԺۺϾƷ| 69SEXþþƷ鶹| 9191ƷѾþ| þþþŮʦһ| ľþþƷ| ƷۺϾþĻ| þ| þþþ޾Ʒ| þþþ18| ƷӰӾþۺ| þ޾Ʒۿ| ξþ99ƷþþþþС˵| þþþù˾Ʒҹ| þþŮ붯ȺëƬ| ޹徫Ʒ߾þ| ݺɫۺϾþ| 18պҹþó | 鶹wwwþ| ˾þô߽ۺAv| ƷȾþëƬ| þѾƷav| þۺƵ| þþþƷƵѹۿ| AëƬþþþƷëƬ| þѵľƷV| ҹþþþþýӰ| þþþþþƷ| 㽶þAһ| þþþרav| ۺѾƷþþ| 99þþƷëƬ| 99Ʒþþþþþ| ĻۺϾþ2| 99þۺϾƷ| MM131޹Ůþ| þAV| þۺϾþۺϾɫ| 91Ʒ91þþþþ| þ޹Ʒһ|