??xml version="1.0" encoding="utf-8" standalone="yes"?>好久久免费视频高清,久久久久久久久波多野高潮,久久成人国产精品一区二区http://m.shnenglu.com/mysileng/category/16815.htmlzh-cnSat, 01 Dec 2012 05:38:03 GMTSat, 01 Dec 2012 05:38:03 GMT60memcpy与memmove(?http://m.shnenglu.com/mysileng/archive/2012/11/25/195649.html鑫龙鑫龙Sun, 25 Nov 2012 08:55:00 GMThttp://m.shnenglu.com/mysileng/archive/2012/11/25/195649.htmlhttp://m.shnenglu.com/mysileng/comments/195649.htmlhttp://m.shnenglu.com/mysileng/archive/2012/11/25/195649.html#Feedback0http://m.shnenglu.com/mysileng/comments/commentRss/195649.htmlhttp://m.shnenglu.com/mysileng/services/trackbacks/195649.html阅读全文

鑫龙 2012-11-25 16:55 发表评论
]]>
(i++)+(i++)?++i)+(++i) (?http://m.shnenglu.com/mysileng/archive/2012/11/19/195380.html鑫龙鑫龙Mon, 19 Nov 2012 12:16:00 GMThttp://m.shnenglu.com/mysileng/archive/2012/11/19/195380.htmlhttp://m.shnenglu.com/mysileng/comments/195380.htmlhttp://m.shnenglu.com/mysileng/archive/2012/11/19/195380.html#Feedback0http://m.shnenglu.com/mysileng/comments/commentRss/195380.htmlhttp://m.shnenglu.com/mysileng/services/trackbacks/195380.html

与在前面:++(--)有太多让人困惑的地方,(i++)+(i++)?++i)+(++i)有什么不?Z么不?如果从机器的角度ȝ解,׃豁然开朗?/p>

 先来看段E序:

复制代码
int main()
{
    
int i=3;
    
int j=(i++)+(i++);
    
//    int j=(++i)+(++i);
    printf("%d,%d\n",i,j);
}
复制代码

(1)在VC 6.0下:

对于(i++)+(i++):
l果Qi=5,j=6

相应的汇~代码ؓ(有详l注?Q?/p>

复制代码
8B 45 FC             mov         eax,dword ptr [ebp-4]   ;i->eax
03 45 FC             add         eax,dword ptr [ebp-4]    ;i+i=6
89 45 F8             mov         dword ptr [ebp-8],eax    ;6->j
8B 4D FC             mov         ecx,dword ptr [ebp
-4]    ;i->ecx(=3)
83 C1 01             add         ecx,1                           ;ecx=4
89 4D FC             mov         dword ptr [ebp-4],ecx    ;4->i
8B 
55 FC             mov         edx,dword ptr [ebp-4]    ;i->edx
83 C2 01             add         edx,1                           ;edx=5
89 55 FC             mov         dword ptr [ebp-4],edx    ;5->i
复制代码

 
对于(++i)+(++i)Q?br />l果Qi=5,j=10
相应的汇~代码ؓQ?/p>

复制代码
8B 45 FC             mov         eax,dword ptr [ebp-4]    ;i->eax (=3)
83 C0 01             add         eax,1                           ;eax=4
89 45 FC             mov         dword ptr [ebp-4],eax    ;4->i
8B 4D FC             mov         ecx,dword ptr [ebp
-4]    ;i->ecx
83 C1 01             add         ecx,1                           ;ecx=5
89 4D FC             mov         dword ptr [ebp-4],ecx    ;5->i
8B 
55 FC             mov         edx,dword ptr [ebp-4]    ;i->edx
03 55 FC             add         edx,dword ptr [ebp-4]    ;edx=10 ,即i+i
89 55 F8             mov         dword ptr [ebp-8],edx    ;10->j
复制代码

 
(2)在gcc 3.2.2?

对于(i++)+(i++):

l果Qi=5,j=6相应的汇~代码ؓQ?/p>

复制代码
c7 45 fc 03 00 00 00     movl    $3, -4(%ebpQ?nbsp;       ;3->i
8b 55 fc        movl    -4(%ebp), %edx        ;i->edx (=3)
8b 45 fc        movl    -4(%ebp), %eax        ;i->eax    (=3)
8d 04 10         leal    (%eax,%edx), %eax     ;i+i=6 ->eax
89 45 f8        movl    %eax, -8(%ebp)        ;6->j
8d 45 fc        leal    -4(%ebp), %eax        ;&i->eax
ff 00            incl    (%eax)            ;i++ ,即i=4,注意q里为寄存器间接d
8d 45 fc        leal    -4(%ebp), %eax        ;&i->eax
ff 00            incl    (%eax)                ;i++,即i=5
复制代码

 
对于(++i)+(++i)Q?br />l果Qi=5,j=10
相应的汇~代码ؓQ?/p>

复制代码
movl    $3, -4(%ebp)        ;3->i
leal    -4(%ebp), %eax        ;&i->eax
incl    (%eax)            ;i++,即i=4
leal    -4(%ebp), %eax        ;&i->eax
incl    (%eax)            ;i++, i=5
movl    -4(%ebp), %eax        ;i->eax, eax=5
addl    -4(%ebp), %eax        ;i+i ->eax ,eax=10
movl    %eax, -8(%ebp)        ;10->j
复制代码


可见Q对于VC6.0和gccQ二者的l果一_但是gcc 3.2.2生成的汇~代码明显比VC6.0高效、简z。这也许是因为VC 6.0出现较早的原因吧?/p>

 

(3)如果q段代码用java实现Q结果会怎样呢?

E序:

复制代码
public class TestAdd {
    
public static void main(String[] args) {
        
int i=3;
        
int j=(i++)+(i++);    //5,7
        
//int j=(++i)+(++i);  //5,9
        System.out.println(i+","+j);
    }
}
复制代码

 对于(++i)+(++i):
i=5,j=9。结果点意外!
来看看它的字节码?

复制代码
//j=(++i)+(++i)
//
5,9
 
0:   iconst_3            ;帔R3入栈
 1:   istore_1            ;从栈中弹?,存入i,i=3
 2:   iinc    11         ;i++, i=4
 5:   iload_1              ;i压入??入栈
 6:   iinc    11          ; i++,i=5
 9:   iload_1               ;i入栈,?入栈
 10:  iadd                  ;从栈中弹Z个intcd的数相加,l果入栈,?入栈
 11:  istore_2            ;从栈中弹?,存入j,即j=9
复制代码

 对于(i++)+(i++):
i=5,j=7。结果也很意?
也来看看它的字节码吧:

复制代码
//j=(i++)+(i++)
//
5,7
 
0:   iconst_3            ;帔R3入栈
 1:   istore_1            ;从栈中弹?,存入i,i=3
 2:   iload_1               ;i入栈,?入栈
 3:   iinc    11          ;i++,即i=4
 6:   iload_1               ;i入栈,?入栈
 7:   iinc    11           ;i++,即i=5;注意Q?没有入栈,所以此时栈中的Cؓ3?
 10:  iadd                  ;从栈弹出两个intcd数相加,l果入栈,?入栈
 11:  istore_2            ;从栈中弹?Q存入j,即j=7
复制代码

 

Java与VC/gccZ么会有如此的区别呢?其实原因很简单,VC/gcc生成的是本地代码Q而X86处理器是Z寄存器的架构Q也是如果它要q行了两个数相加Q它会先把两个数Ud寄存器,再进行加法运。而Java虚拟机是一U基于栈的架构,如果它要q行两个数相加,它会先弹Z个数Q再q行加法q算Q再结果入栈?/p>

鑫龙 2012-11-19 20:16 发表评论
]]>
2l数l和指针差异http://m.shnenglu.com/mysileng/archive/2012/01/28/164611.html鑫龙鑫龙Sat, 28 Jan 2012 11:40:00 GMThttp://m.shnenglu.com/mysileng/archive/2012/01/28/164611.htmlhttp://m.shnenglu.com/mysileng/comments/164611.htmlhttp://m.shnenglu.com/mysileng/archive/2012/01/28/164611.html#Feedback0http://m.shnenglu.com/mysileng/comments/commentRss/164611.htmlhttp://m.shnenglu.com/mysileng/services/trackbacks/164611.html
数组和指针概念很Ҏ让初学者؜淆,q里把我所知道的概括一下,希望对大家有所帮助?/div>

一l数l和一U指针的相同和差?/h2>

一l数l的名字大部分情况下可以当成一个常指针来看Q也是_

int a[3] int * const p;

则数l名a可以在大部分p能用的地方用,有两U情冉|较特D:

  1. p可以在定义是初始化一个地址如:int *p const = new int[100];Q而且q也是唯一的初始化他的Z。而数l则是有操作pȝ载入可执行文件时初始化内存空间的?/li>
  2. sizeofq算W对p计算获得指针的长度Q现在一般ؓ4Q,而对数组名则得到数组占有的所有字节数?/li>

在内存排布上Q一l数l和一阶指针指向的内存是完全一L?/p>

高维数组和高阶指?/h2>

对于高维数组Q以二维ZQ其他完全一PQ情况和一l数l完全不一栗例如对于数l和指针Q?/p>

int a[3][4] int**p;

他们没有Q何可比性。初学者从一l数l中的知识简单的推断出a是一个等价于int**的东西,*a可以得C?int*的|q其实是完全错误的?/p>

从内存排布上Q数l按照先低维后高l的序一ơ排列数l的每个元素。其中低l到高维是指定义数组ӞLl名近的维Zl_反之为高l_例如上面q个数组Q?是低维的维敎ͼ4为高l的l数Q因此,在内存中Q上面这个数l占?2个int单元Q所有单元靠在一P光序则?/p>

a[0][0]Qa[0][1]Qa[0][2]Qa[0][3] a[1][0]Qa[1][1]Qa[1][2]Qa[1][3] a[2][0]Qa[2][1]Qa[2][2]Qa[2][3]

如果a是一个和二阶指针{h的常量,那么他的值应该指向一个指针数l才对,而实际上q样的指针数l是不存在?/p>

从前面这个排布还可以看出一个问题,在排列过E中Q数l的最低维不确定是没有关系的,但是Z安排好数l元素的先后序Q高l的l数必须定Q也是说在使用数组cd来定义指针时Q必首先确定高l_例如Q?/p>

int (*p)[3] = new int[4][3]

是可以的Q但?/p>

int (*p)[] = new int[4][3]

׃行?/p>

回到一l数l?/h2>

仔细考虑一l数l和多维数组Q是不是~译器对一l数l特D处理了Q其实也不是Q数l名其实代表的是数组对象也就是其W一个元素的地址Q从q个角度Ԍ一l数l名a代表了a[0]的地址Q二l数l名a代表了a[0][0]的地址Q高l数l的名字从这个意义讲更接q?阶指?



鑫龙 2012-01-28 19:40 发表评论
]]>如何二l数l?静态的和动态的)作ؓ函数的参C?/title><link>http://m.shnenglu.com/mysileng/archive/2012/01/28/164610.html</link><dc:creator>鑫龙</dc:creator><author>鑫龙</author><pubDate>Sat, 28 Jan 2012 11:24:00 GMT</pubDate><guid>http://m.shnenglu.com/mysileng/archive/2012/01/28/164610.html</guid><wfw:comment>http://m.shnenglu.com/mysileng/comments/164610.html</wfw:comment><comments>http://m.shnenglu.com/mysileng/archive/2012/01/28/164610.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://m.shnenglu.com/mysileng/comments/commentRss/164610.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/mysileng/services/trackbacks/164610.html</trackback:ping><description><![CDATA[<div><span style="color: #74bc2f; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; "><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">今天写程序的时候要用到二维数组作参Cl一个函敎ͼ我发现将二维数组作参数进行传递还不是惌得那么简单,但是最后我也解决了遇到的问题,所以这文章主要介l如何处理二l数l当作参C递的情况Q希望大家不至于再在q上面浪Ҏ间?/span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">      <span style="line-height: 25px; ">下文是我从互联网上download的一文章,讲的很好Q但是我后面指出问题所在,q加以改q,希望对你有所帮助Q?/span><br style="line-height: 25px; " /><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">首先Q我引用了K强先生~著的?span style="line-height: 34px; ">C</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">E序设计》上面的一节原文,它简要介l了如何二l数l作为参C递,原文如下<span style="line-height: 34px; ">(</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">略有改变Q请原谅<span style="line-height: 34px; ">)</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">Q?/span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">[</span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">原文开?span style="line-height: 34px; ">]</span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">可以用二l数l名作ؓ实参或者Ş参,在被调用函数中对形参数组定义时可以指定所有维数的大小Q?span style="line-height: 34px; ">也可以省略第一l的大小说明</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">Q如Q?/span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int array[3][10]);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int array[][10]);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">二者都是合法而且{hQ?span style="line-height: 34px; ">但是不能把第二维或者更高维的大省?/span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">Q如下面的定义是不合法的Q?/span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int array[][]);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">因ؓ从实参传递来的是数组的v始地址Q在内存中按数组排列规则存放<span style="line-height: 34px; ">(</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">按行存放<span style="line-height: 34px; ">)</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">Q而ƈ不区分行和列Q如果在形参中不说明列数Q则pȝ无法军_应ؓ多少行多列Q?span style="line-height: 34px; ">不能只指定一l而不指定W二l_下面写法是错误的Q?/span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int array[3][]);</span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">实参数组l数可以大于形参数组Q例如实参数l定义ؓQ?/span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int array[3][10]);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">而Ş参数l定义ؓQ?/span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">int array[5][10];</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">q时形参数组只取实参数组的一部分Q其余部分不起作用?/span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">[</span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">原文l束<span style="line-height: 34px; ">]</span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">大家可以看到Q将二维数组当作参数的时候,必须指明所有维数大或者省略第一l的Q但是不能省略第二维或者更高维的大,q是q译器原理限制的。大家在学编译原理这么课E的时候知道编译器是这样处理数l的Q?/span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">对于数组<span style="line-height: 34px; "> int p[m][n];</span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">如果要取<span style="line-height: 34px; ">p[i][j]</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">的?span style="line-height: 34px; ">(i>=0 && i<m && 0<=j && j < n)</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">Q编译器是这样寻址的,它的地址为:</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">p + i*n + j;</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">从以上可以看出,如果我们省略了第二维或者更高维的大,~译器将不知道如何正的d。但是我们在~写E序的时候却需要用到各个维数都不固定的二维数组作ؓ参数Q这难办了Q编译器不能识别阿,怎么办呢Q不要着急,~译器虽然不能识别,但是我们完全可以不把它当作一个二l数l,而是把它当作一个普通的指针Q再另外加上两个参数指明各个l数Q然后我们ؓ二维数组手工dQ这样就辑ֈ了将二维数组作ؓ函数的参C递的目的Q根据这个思想Q我们可以把l数固定的参数变为维数随即的参数Q例如:</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    void Func(int array[3][10]);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int array[][10]);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">变ؓQ?/span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Func(int **array, int m, int n);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">在{变后的函CQ?span style="line-height: 34px; ">array[i][j]</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">q样的式子是不对?span style="line-height: 34px; ">(</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">不信Q大家可以试一?span style="line-height: 34px; ">)</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">Q因为编译器不能正确的ؓ它寻址Q所以我们需要模仿编译器的行为把<span style="line-height: 34px; ">array[i][j]</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">q样的式子手工{变ؓQ?/span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">*((int*)array + n*i + j);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">在调用这L函数的时候,需要注意一下,如下面的例子Q?/span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">int a[3][3] =</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">      </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{1, 1, 1},</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">      </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{2, 2, 2},</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">      </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{3, 3, 3}</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">};</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">Func(a, 3, 3);</span><br style="line-height: 25px; " /></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">Ҏ不同~译器不同的讄Q可能出?span style="line-height: 34px; ">warning </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">或?span style="line-height: 34px; ">error,</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">可以q行强制转换如下调用Q?span style="line-height: 34px; "> </span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">Func((int**)a, 3, 3);</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">  </span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">其实多维数组和二l数l原理是一LQ大家可以自己扩充的多维数组Q这里不再赘q。写到这里,我先向看了这文章后悔的人道歉,费你的旉了。下面是一个完整的例子E序Q这个例子程序的主要功能是求一个图中某个顶点到其他点的最短\l,图是以邻接矩늚形式存放?span style="line-height: 34px; ">(</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">也就是一个二l数l?span style="line-height: 34px; ">)</span></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">Q其实这个函C是挺有用的,但是我们q篇文章的重点在于将二维数组作ؓ函数的参C递?/span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">   <span style="line-height: 34px; "> 上文l束Q上文最后指Z实现二位数组作ؓ函数参数的方法,但是它实现的是将静态的二位数组作ؓ参数Q但是如何将动态而ؓ数组作ؓ参数呢?上面的方法显然是不合适的Q下面是我琢出来的Ҏ?/span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; color: #ff0000; ">    <span style="line-height: 34px; ">先将静态数l作为参数的代码贴出?</span></span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">#include <iostream><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">using namespace std;<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Calc(int **A, int m, int n);<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">int _tmain(int argc, _TCHAR* argv[])<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int row = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int col = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int i = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int A[3][3];<br style="line-height: 34px; " />    <br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    for (row = 0; row < 3; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        for (col = 0; col < 3; col++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            A[row][col] = row + col;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " />   <span style="line-height: 34px; "> Calc((int **)A, 3, 3);</span><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    return 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">}<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Calc(int **A, int m, int n)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    if (NULL == A || m <1 || n < 1)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        return;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int row = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int col = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int i = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int j = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int **matrix = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    matrix = new int*[m];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    if (NULL == matrix)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        return;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    for (row = 0; row < m; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        matrix[row] = new int[n];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        if (NULL == matrix[row])<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            for (i = 0; i < row; i++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">                delete []matrix[i];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">                matrix[i] = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            delete []matrix;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            matrix = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            return;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    for (row = 0; row < m; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        for (col = 0; col < n; col++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        {<br style="line-height: 34px; color: #ff0000; " /><span style="line-height: 34px; ">            matrix[row][col] = *((int *)A + row * n + col);</span><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            //matrix[row][col] = A[row][col];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " />    <br style="line-height: 34px; " />    <br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    for (row = 0; row < m; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        for (col = 0; col < n; col++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            cout<<matrix[row][col]<<"  ";<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        cout<<"\n";<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">}</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">下面是动态二位数l作为函数参数时的代码:</span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">#include <iostream><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">using namespace std;<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Calc(int **A, int m, int n);<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">int main(int argc, char* argv[])<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int row = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int col = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int i = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int **A = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    A = new int*[3];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    if (NULL == A)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        return 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    for (row = 0; row < 3; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        A[row] = new int[3];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        if (NULL == A[row])<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            for (i = 0; i < row; i++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">                delete []A[i];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">                A[i] = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            delete []A;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            A = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            return 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " />    <br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    for (row = 0; row < 3; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        for (col = 0; col < 3; col++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            A[row][col] = row + col;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " />   <span style="line-height: 34px; "> Calc((int **)A, 3, 3);</span><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    return 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">}<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">void Calc(int **A, int m, int n)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">{<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    if (NULL == A || m <1 || n < 1)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        return;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " /><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int row = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int col = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int i = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int j = 0;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    int **matrix = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    matrix = new int*[m];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    if (NULL == matrix)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        return;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    for (row = 0; row < m; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        matrix[row] = new int[n];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        if (NULL == matrix[row])<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            for (i = 0; i < row; i++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">                delete []matrix[i];<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">                matrix[i] = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            delete []matrix;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            matrix = NULL;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            return;<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    for (row = 0; row < m; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        for (col = 0; col < n; col++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            //matrix[row][col] = *((int *)A + row * n + col);<br style="line-height: 34px; " />            <span style="line-height: 34px; ">matrix[row][col] = A[row][col];</span><br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " />    <br style="line-height: 34px; " />    <br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    for (row = 0; row < m; row++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        for (col = 0; col < n; col++)<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        {<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">            cout<<matrix[row][col]<<"  ";<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">        cout<<"\n";<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">    }<br style="line-height: 34px; " /></span><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; ">}<br style="line-height: 34px; " />   <span style="line-height: 34px; "> 注意上面的代码的不同之处Q即动态二l数l作为函数参数时Q在函数里面应用时候要其伪装成静态二l数l!</span></span></p><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="line-height: 34px; font-size: 14pt; font-family: 楷体_GB2312; "><span style="line-height: 34px; ">    q样Q以上的两段代码不仅实现了堆和栈之间数据的传递,而且实现了堆和堆之间数据的传递!</span></span></p></span></div><img src ="http://m.shnenglu.com/mysileng/aggbug/164610.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/mysileng/" target="_blank">鑫龙</a> 2012-01-28 19:24 <a href="http://m.shnenglu.com/mysileng/archive/2012/01/28/164610.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TCPL学习W记-----W??-----(l构) http://m.shnenglu.com/mysileng/archive/2011/09/09/155456.html鑫龙鑫龙Fri, 09 Sep 2011 07:39:00 GMThttp://m.shnenglu.com/mysileng/archive/2011/09/09/155456.htmlhttp://m.shnenglu.com/mysileng/comments/155456.htmlhttp://m.shnenglu.com/mysileng/archive/2011/09/09/155456.html#Feedback0http://m.shnenglu.com/mysileng/comments/commentRss/155456.htmlhttp://m.shnenglu.com/mysileng/services/trackbacks/155456.html6.1l构的基本知?br />
6.2l构与函?br />p114---(1)不同的结构体变量之间q行赋gq行单的拷贝g递?br />p115---(2)l构q算W?." ?->",用于函数调用?()"以及用于下标?[]"的算术符优先U最高?br />
6.3l构数组
p116---(1)l构体数l初始化
  struct key {
      char *word;
      int    count;
  }keytab[] = {
      {"auto",0},
      {"break",0},
      {"case",0},
      {"while",0}
  };
p118---(2)计算l构体数l中的结构体个数Ӟ需要用到sizeof, sizeof keytab / sizeof(struct key) 。另外条件编译语?if中不能用sizeofQ因为预处理器部队类型名q行分析.但预处理器ƈ不计?define语句中的表达?因此?define中用sizeof是合法的?br />
6.4指向l构的指?br />p120---(1)两个指针之间的加法运是非法的。但?指针的剑法运却是合法的,mid=(low+high)/2是错误的,但是mid=low+(high-low)/2可以了?br />p115---(2)千万不要以ؓl构的长度等于各成员长度的和。因Z同的对象有不同的寚w要求Q所以,l构中可能会出现未命名的“I穴”。一般需要用sizeof来正获得结构体长度?br />
6.5自引用结?br />
6.6表查?br />
6.7cd定义(typedif)
p127---(1)typedef中声明的cd在变量位|出玎ͼ而不是紧接在关键字typedef之后。typedef在语法上cM与extern{?br />p128---(2)typedefcM?define语句Q但׃typedef是由~译器解释的Q因此它的问题替换功能要过预处理器的能力。例? typedef int (*PFI)(char *,char*);

6.8联合
p128---(1)联合提供了一U方式,以在单块存储Z理不同cd的数据,目的在于一个变量可以合法的保存多种数据cd中的M一U类型?br />p129---(2)联合体变量读取的cd必须是最q一ơ存入的cd。程序员赋Dt当前保存在联合中的cdQ如果保存的cd和读取的cd不一_其结果取决于具体的实现?br />p130---(3)联合是一个结构,他得所有成员相对于基地址的偏U量都是0Q此l构I间要大到够容Ux“?#8221;的成员?br />
6.9位字D?br />p131---(1)struct {
                 unsigned int is_keyword : 1;
                 unsigned int is_extern   : 1;
               }flag;
q里定义了一个变量flagQ它包含3?位的字段。冒号后的数字表C字D늚宽带(2q制)?br />flag.is_keyword = 1;


鑫龙 2011-09-09 15:39 发表评论
]]>
TCPL学习W记-----W??-----(指针与数l?http://m.shnenglu.com/mysileng/archive/2011/07/21/151553.html鑫龙鑫龙Thu, 21 Jul 2011 08:07:00 GMThttp://m.shnenglu.com/mysileng/archive/2011/07/21/151553.htmlhttp://m.shnenglu.com/mysileng/comments/151553.htmlhttp://m.shnenglu.com/mysileng/archive/2011/07/21/151553.html#Feedback0http://m.shnenglu.com/mysileng/comments/commentRss/151553.htmlhttp://m.shnenglu.com/mysileng/services/trackbacks/151553.html5.1指针与地址
p79---(1)指针是能够存放一个地址的一l存储单元,通常是两个或者四个字节?br />p80---(2)int *ip Q该声明语句表明*ip的结果是一个intcd?br />
5.2指针与函数参?br />
5.3指针与数l?br />p84---(1)如果pa指向数组中的某个特定元素Q那么根据指针运的定义Qpa+1指向下一个元素,pa+i指向pa所指向数组元素之后的第i个元素。例?(pa+i){同于a[i]?br />p85---(2)我们必须CQ数l名和指针之间有一个不同之处。指针是一个变量,因此Q在c语言中,语句pa=a和pa++都是合法的。但数组名不是变量,因此Q类ga=paQa++的语句都是非法的?br />p86---(3)如果信相应的元素存在,也可以通过下标讉K数组W一个元素之前的元素。类|p[-1]、p[-2]q样的表辑ּ在语法上也是合法的,它们分别引用位于p[0]之前的两个元素,当然引用数组边界之外的对象是非法的?br />
5.4地址术q算
p87---(1)c语言保证Q?永远不是有效的数据地址Q因此返回?可用来表C发生了异常事g?br />p88---(2)指针与整C间不能相互{换,?是惟一的例外:帔R0可以赋值给指针Q指针也可以和常?q行比较。程序中l常用符号常量NULL代替帔R0Q这样便于更清晰地说明常?是指针的一个特D倹{符号常量NULL定义在标准头文g<stddef.h>中?br />p89---(3)有效的指针运包括相同类型指针之间的赋D;指针同整C间的加法或减法运;指向相同数组中元素的两个指针间的减法或比较运,指针赋gؓ0或指针与0之间的比较运。其他所有Ş式的指针q算都是非法的,例如两个指针间的加法Q乘法,除法Q移位或屏蔽q算Q指针同float或doublecd之间的加法运;不经强制cd转换而直接将指向一U类型对象的指针赋值给指向另一U类型对象的指针q算?br />
5.5字符指针与函?br />p90---(1)注意char amessage[] = "now"; 和char *pmessage = "now";的区别,amesaage是一个仅仅存攑ֈ始化字符串以及空字符'\0'的一l数l。它始终指向同一个存储位|。而pmeesage是一个指针,其初值指向一个字W串帔RQ之后还可以被修改以指向其他地址?br />
5.6指针数组以及指向指针的指?br />p94---(1)char *lineptr[MAXLINES]是一个指针数l,lineptr[i]是一个字W指针,?lineptr[i]是该指针指向的第i个文本行的首字符?br />


5.7多维数组
p95---(1)char daytab[2][3] =  {{1,2,3},{1,2,3}};
p96---(2)如果二l数l作为参C递给函数Q那么在函数的参数声明中必须指明数组的列数?/span>
p97---(3)特别注意Q方括号[]的优先高于*的优先Q所以int (*day)[13] ?int *day[13]是完全不同的含义。前者是表示一个二l数l,每个元素放intcdQ后者表CZl指针数l,里面攄是指向一个intcd的地址?/span>


5.8
指针数组的初始化


5.9指针与多l数l?/font>

p98---(1)到目前ؓ止,指针数组最频繁的用处是存放h不同长度的字W串


5.10命o行参?/font>

p98---(1)调用d?/span>mainӞ它有两个参数。第一个参?/span>argc的DC行程序时命o行中参数的数目;W二个参?/span>argv是一个指向字W串数组的指针,其中每一个字W串对应一个参数?/span>

p99---(2)按照c语言的约定,argv[0]的值是启动E序的程序名Q因?/font>argc的Dؓ1.如果argc的gؓ1Q则说明E序后面没有命o行参数。第一个可选的参数?/font>argv[1],而最后一个可选参Cؓargv[argc-1]。另外,ANSI标准要求argv[argc]必须Z个空指针?/font>

p101---(3)注意(*++argv)[0]?/font>*++argv[0]的区别,前者是行移动,后者是列移动?/font>

 

5.11指向函数的指?/font>

p104---(1)注意int (*comp)(void *,void *)?/font>int *comp(void *,void *)的区别,前?/font>comp是一个函数指针指向一个返回类型ؓint的函敎ͼ而后?/font>comp为函数名q回cd?/font>int指针?/font>


5.12复杂声明Q这节后面的E序有点复杂Q直接无视,哈哈~~Q?nbsp;

p106---(1)char (*(*x())[])() ,x是一个无参函敎ͼq回一个指针,指针指向一个数l,数组中的元素也是指针Q指向一个返回类型ؓchar的无参函数?/font>

p106---(2)char (*(*x[3])())[5] ,x是一个长度ؓ3的数l,数组中的元素是指针,指向一个无参的函数Q该函数q回cd也是指针Q指向一个长度ؓ5?/font>charcd数组?/font>



鑫龙 2011-07-21 16:07 发表评论
]]>
TCPL学习W记-----W??-----(函数与程序结?http://m.shnenglu.com/mysileng/archive/2011/06/07/148207.html鑫龙鑫龙Tue, 07 Jun 2011 07:47:00 GMThttp://m.shnenglu.com/mysileng/archive/2011/06/07/148207.htmlhttp://m.shnenglu.com/mysileng/comments/148207.htmlhttp://m.shnenglu.com/mysileng/archive/2011/06/07/148207.html#Feedback0http://m.shnenglu.com/mysileng/comments/commentRss/148207.htmlhttp://m.shnenglu.com/mysileng/services/trackbacks/148207.html4.1函数的基本知?br />p59---(1)如果函数定义中省略了q回cdQ则默认为intcd。函C间的通信可以通过参数、函数返回倹{外部变量进行?br />p60---(2)假定有三个函数分别存攑֜名ؓmain.c、getline.c、strindex.c的三个文件中Q则可以使用命oQcc main.c getline.c strindex.c 来编译这3个文Ӟq把生成的目标代码分别存攑֜文gmain.o、getline.o、strindex.o中,然后再把q?个文件一起加载到可执行文件a.out中。如果源文g中存在错误(比如文gmain.c中存在错误),则可以通过命o:cc main.c getline.o strindex.o 对main.c重新~译Qƈ编译的l果与以前已~译q的目标文ggetline.o、strindex.o一起加载到可执行文件中。cc命o使用".c"?.o"q两个拓展名来区分源文g和目标文件?br />
4.2q回非整型值的函数
p61---(1)函数的声明与定义必须一致。如果函C调用它的d数main攑֜同一个源文g中,q且cd不一_~译器就会检到该错误。但是如果函数是单独~译的,q种不匹配的错误无法检出来,函数返回非intcd的|而main函数却将q回值按照intcd处理Q最后结果将变得毫无意义。实验如下:
test1.c                                   test2.c
#include<stdio.h>                   #include<stdio.h>
double f(){                             void main(){
 return 1.1;                                 int i = f();
}                                               printf("%d\n",i); 
                                            }
通过cc test1.c test2.c~译链接生成a.out,q行i的结果是毫无意义的?l果q不?,不知道ؓ什么。。?

4.3外部变量
p66---(1)׃E序要超前读入一个字W,q样导致最后有一个字W不属于当前所要读入的数。如果能“反读”不需要的字符Q该问题可以得到解冟뀂标准库提供了函数ungetc,它将一个字W压回到~冲中?br />
4.4作用域规?br />p68---(1)对于在函数开头声明的自动变量来说。其作用域是声明该变量名的函敎ͼ不同函数中声明的h相同名字的各个局部变量之间没有Q何关pR外部变量的作用域从声明它的地方开始,到其所在的文g(待编?的末束?br />p68---(2)外部变量的声明与定义严格区分开来很重要。变量声明用于说明变量的属?主要是类?Q而变量定义除此之外还引起存储器的分配。通常局部变量和不加extern的外部变量是集声明与定义于一体的?/span>
p69---(3)在一个源E序的所有源文g中,一个外部变量只能在某个文g中定义一ơ,而其他文件可以通过extern声明来访问。外部变量的定义中必d定数l的长度Q但extern声明则不一定要制定数组的长度?br />
4.5头文?br />p70---(1)我们对下面两个因素进行了折中Q一斚w是我们期望每个文件只能访问它完成d所需要的信息Q另一个方面是现实中维护较多的头文件比较困难。我们可以得样一个结论:对于某些中等规模的程序,最好只用一个头文g存放E序中各部分׃n的对象。较大的E序需要用更多的头文Ӟ我们需要精心组l它们?br />
4.6静态变?br />p70---(1)用static声明来限定外部变量与函数Q可以将其后声明的对象的作用域限制ؓ被编译源文g的剩余部分,以达到对外隐藏的目的?br />p71---(2)static也可以用来声明内部变量,staticcd的内部变量同自动变量一P是某个特定函数的局部变量,只能在该函数中用,但它与自动变量不同的是,不管其所在函数是否被调用Q它一直存在,而不像自动变量那P随着所在函数的被调用和退存在和消失。换句话_staticcd的内部变量是一U只能在某个特定函数中用但一直占据存储空间的变量?br />
4.7寄存器变?br />p71---(1)register声明只适用于自动变量以及函数的形式参数。它所声明的变量在E序中用频率较高,思想是将register变量攑֜机器的寄存器中?br />p71---(2)无论寄存器变量实际上是不是存攑֜寄存器中Q它的地址都是不能讉K的。在不噢那个的机器中Q对寄存器变量的数目和类型的具体限制也是不同的?br />4.8E序块结?br />

4.9初始?br />p72---(1)在不昑ּ初始化的情况下,外部变量和静态变量都被隐式初始化ؓ0Q而自动变量和寄存器变量的初值则没有意义?br />p72---(2)外部变量与静态变量来_初始化的表达式必L帔R表达?即不能是之前已经定义q的??br />

4.10递归

4.11C预处理器
4.11.1文g包含
p75---(1)#include的行被替换为由文g名指定的文g的内宏V如果文件名用引号引hQ则在源文g所在位|查找该文gQ如果没有找到文Ӟ或者用尖括号把文件名引v来,则将Ҏ相应的规?查找该文Ӟq个规则同具体的实现有关?br />

4.11.2宏替?br />p76---(1)通常情况下,#define指定占一行,替换文本?define指定行尾部的所有剩余部分内容,但也可以把一个较长的宏定义分成若q行Q这旉要在待箋的行末尾加上一个反斜杠W\?br />p76---(2)宏定义也可以带参敎ͼ如define max(A,B) ((A)>(B)?(A):(B)),调用时候只需要x = max(i,j)可以。但仔细考虑一下max的展开式,׃发现存在一些缺P其中表达式要重复计算2ơ,那么假如q样调用x = max(i++,j++)l果会i或j自增两次?br />p77---(3)预处理器q算W?#提供了一U连接实际参数的手段。如果替换文本中的参C##盔RQ则该参数将被实际参数替换,##与前后的I白W将被删除,q对替换后的l果重新扫描。例?#define paster(f,b) f##b Q调用的时候paster(i,5)l果是i5的参数?br />

4.11.3条g包含
p78---(1)#if语句对其中的帔R整Ş表达式(不能包含sizeof、类型{换、enum帔RQ进行求|若表辑ּ的值非0则包含其后各行,直到遇到#endif\#elif?else语句为止?br />q有其他?ifdef?ifndef?endif{等。。。?/p>



鑫龙 2011-06-07 15:47 发表评论
]]>
TCPL学习W记-----W??-----(控制? http://m.shnenglu.com/mysileng/archive/2011/05/07/145891.html鑫龙鑫龙Sat, 07 May 2011 05:38:00 GMThttp://m.shnenglu.com/mysileng/archive/2011/05/07/145891.htmlhttp://m.shnenglu.com/mysileng/comments/145891.htmlhttp://m.shnenglu.com/mysileng/archive/2011/05/07/145891.html#Feedback0http://m.shnenglu.com/mysileng/comments/commentRss/145891.htmlhttp://m.shnenglu.com/mysileng/services/trackbacks/145891.html3.1语句与程序块
p46---(1)双括号用于l束E序块,其后不需要分受?br>
3.2if-else语句

3.3else-if语句

3.4switch语句

3.5while循环与for循环

3.6do-while循环

3.7break语句与continue语句

3.8goto语句与标?br>
p54---(1)从理Z上,goto语句是没有必要的。但是,在某些场合下goto语句q是用得着的。最常见的用法就是终止程序在某些深度嵌套的结构中的处理过E,例如一ơ蟩Z层或多层循环。这U情况用break是不能达到目的的Q它只能从最内层循环退出到上一U的循环?br>
以下是习题部分:
1.~写函数itoa(n,s),整数n转化为字W串Q包括负数?br> 

鑫龙 2011-05-07 13:38 发表评论
]]>
TCPL学习W记-----W??-----(cd、运符与表辑ּ)http://m.shnenglu.com/mysileng/archive/2011/05/05/145770.html鑫龙鑫龙Thu, 05 May 2011 12:35:00 GMThttp://m.shnenglu.com/mysileng/archive/2011/05/05/145770.htmlhttp://m.shnenglu.com/mysileng/comments/145770.htmlhttp://m.shnenglu.com/mysileng/archive/2011/05/05/145770.html#Feedback0http://m.shnenglu.com/mysileng/comments/commentRss/145770.htmlhttp://m.shnenglu.com/mysileng/services/trackbacks/145770.html2.1变量?br />p27---(1)对变量的命名与符号常量的命o存在一些限制条件。名字由字母和数字组成的序列Q但其第一个字W必Mؓ字母。下划线"_"被看成是字母Q通常用于命名较长的变量名Q以提高其可L。由于库例程的名字通常以下划线开_因此变量名不要以下划U开头?br />
2.2数据cd及长?br />p28---(1)shortcd通常?6位,longcd通常?2位,intcd可以?6位或?2位。各~译器可以根据硬件特性自主选择合适的cd长度Q但要遵守下列限Ӟshort与intcd臛_?6位,而longcd臛_?2位,q且shortcd不得长于intcdQ而intcd不得长于longcd?/p>
2.3帔R
p28---(1)帔R分ؓ整数帔R、QҎ帔R、字W串帔R、字W常量以及枚丑ָ量。其中字W常量还可以使用转义字符序列代替?br />p28---(2)默认的普通整数常量是intcdQQҎ帔R时doublecd。当整数帔R过intcd的范围会自动被当做longcd处理。如果不过范围时候以字母ll尾也将被当成longcd帔R。若后缀是f代表floeat型。若后缀是u代表unsigned无符h。(ul代表unsigned longQ?br />p31---(3)枚Dcd格式: enum boolean {NO=0,YES};惛_?define语句来说Q它的优势常量值可以自动生成,而且使用变量存储q些值得时候,枚D变量q提供是否ؓ该枚丄型的有效值的查,因此枚D?define更具优势?br />
2.4声明
p31---(1)所谓的声明是extern int a;但是~译器ƈ不会为a分配内存即不会压入栈中,只有当int a =1Q被执行及初始化或者叫定义以后才会被分配内存?通常认ؓint a=1集声明与定义于一??br />p31---(2)未经昑ּ初始化的变量的gؓ未定义值即无效倹{?br />p31---(3)M变量的声明都可以使用const限定W限定。该限定W指定变量的g能被修改。对数组而言Qconst限定W指定数l所有元素的值都不能被修攏Vconst限定W也可以配合数组参数使用Q它表明函数不能修改数组元素的倹{?br />
2.5术q算W?br />p32---(1)取模q算W?不能应用于float或doublecd。在有负操作数的情况下,整数除法截取的方向以及取模运结果的W号取决于具体机器的实现?br />
2.6关系q算W与逻辑q算W?br />
2.7cd转换
p33---(1)自动转换是指把比较窄的操作数转换成比较宽的操作数Qƈ且不丢失信息的{换。而如果把比较宽的操作数{换成比较H的操作数通常需要用到强制类型{换?br />p34---(2)C语言定义保证了机器的标准打印字符集中的字W不会是负|因此Q在表达式中q些字符L正倹{但是,存储在字W变量中的值理Z是可以ؓ正也可以的。ؓ了保证程序的可移植性,如果要在charcd的变量中存储非字W数据,最好指定signed或者unsigned限定W?br />p35---(3)一般来_如果二元q算W的两个操作数具有不同的cdQ那么在q行q算之前先把“较低”的类型提升ؓ“较高”的类型。且q算l果?#8220;较高”的类型。但是如果两个操作数中包含unsignedcd的操作数Ӟ转换规则要复杂一些,因ؓ带符号g无符号g间的比较q算是与机器有关的。例?1L?U比较大小Q如果把两者都提升为有W号数则后者大Q如果把两者都提升为无W号的数则前者较大?br />
2.8自增q算W与自减q算W?br />
2.9按位q算W?br />p38---(1)C语言提供?个位操作q算W。这些运符只能作用于整数操作数Q即有符h者无W号的char\short\int\long?U分别是:&、|、^、~?gt;>?lt;<?br />p39---(2)对于>>左移Ӟ有符h高位用符号位填补、无W号数高位用0填补。对?lt;<右移Ӟ一律用0填补?br />
2.10赋D符与表辑ּ

2.11条g表达?br />
2.12q算W优先与求值次?br />

以下是练习题部分Q?br />1. ~写一个程序以定分别由signed及unsigned限定的char、short、int、longcd变量的取D围?br />
2.~写函数htoi(s),把由十六q制数字l成的字W串转ؓZ之等L整型倹{?br />
3.
~写一个函数setbits(x,p,n,y)Q该函数q回对x执行下列操作后的l果|x中从Wp为开始的n个位讄成y中最双n位的|x其余各位保持不变?


鑫龙 2011-05-05 20:35 发表评论
]]>
TCPL学习W记-----W??-----(D)http://m.shnenglu.com/mysileng/archive/2011/05/03/145606.html鑫龙鑫龙Tue, 03 May 2011 14:29:00 GMThttp://m.shnenglu.com/mysileng/archive/2011/05/03/145606.htmlhttp://m.shnenglu.com/mysileng/comments/145606.htmlhttp://m.shnenglu.com/mysileng/archive/2011/05/03/145606.html#Feedback0http://m.shnenglu.com/mysileng/comments/commentRss/145606.htmlhttp://m.shnenglu.com/mysileng/services/trackbacks/145606.html1.1入门
p3---(1)h意,\n只代表一个字W。类g\n的{义字W序列ؓ表示无法输入的字W或不可见字W提供一U通用的可扩充机制。例?\t表示制表W、\b表示回退W、\"表示双引受\\表示反斜杠、\n表示换行W、\r表示回RW?{?.....

1.2变量与算术表辑ּ
p4---(1)在C语言中,所有变量都必须先声明后使用。声明通常攑֜函数的v始处Q在M可执行语句之前?br>p4---(2)对于intcd而言Q通常?6位,其取D围ؓ-32768~32767之间Q也有用32位表C的intcd?br>            
p5---(3)charcd?span style="COLOR: red">一个字?/span>。整数除法操作将执行舍位Q结果中的Q何小数部分都会被舍弃?br>p6---(4)printf函数q不是C语言本n的一部分QC语言本nq没有定义输入输出功能。printf仅仅是标准函数库中一个有用的函数而已。其?d表示整数?s表示字符丌Ӏ?o表示八进制?x表示十六q制数?c表示字符?%表示癑ֈhw。如果在%后面加数字,表示打印宽度Q打印的内容会在打印区域叛_齐)。特别的?f表示点?如果有小数点和数字的q列的话Q小数点后的数字代表数点后的显C精度,?%6.2f?br>1.3for语句

1.4W号帔R
p9---(1)#define 名字 替换文本。注意替换文本可以是M字符序列Q而不仅限于数字。当然此指o的末没有分?br>
1.5字符输入/输出
p9---(1)标准库提供的输入输出模型非常单。无论是从何处输入,输出C处,其输入输出都是按?span style="COLOR: red">字符?/span>的方式处理。文本流是由多行字符构成的字W序列,而每行字W则?个或者多个字W构成,行末是一个换行符(\n)?br>
1.5.1文g复制
p10---(1)因ؓ某些潜在的重要原因,我们在此使用intcd来接受getchar()的返回倹{之所以不把其用charcd装蝲是因为它必须_大,除了能存储Q何可能的字符外还要能存储文g的结束符EOF?br>
1.5.2字符计数

1.5.3行计?br>
1.5.4单词计数

1.6数组

1.7函数
p18---(1)函数定义可以以Q意次序出现在一个源文g或多个源文g中,但同一个函C能分割存攑֜多个文g中。如果源E序分割分散在多个文件中Q那么,在编译和加蝲Ӟ需要做更多的工作,但这是操作系l的原因Qƈ不是语言的属性决定的?br>p18---(2)main函数的末有一个return语句。由于main本n也是函数Q因此也可以向其调用者返回一个|该调用者实际上是E序的执行环境。一般来_q回gؓ0表示正常l止Q返回gؓ?表示出现异常情况或出错结束条件?br>
1.8参数——传D?br>p19---(1)在C语言中,所有函数参数都?#8220;通过?#8221;传递的。也是_传递给被调用函数的参数值存攑֜临时变量中,而不是存攑֜原来的变量中?br>p20---(2)必要Ӟ也可以让函数能够修改主调用函C的变量。这U情况下Q调用者需要向被调用函数提供待讄值得变量的地址Q也是指针Q?br>
1.9字符数组
p22---(1)当在C语言E序中出现类g"hello\n"的字W串帔RӞ它将?span style="COLOR: red">字符数组的Ş?/span>存储Q数l的各元素分别存储字W串的各个字W,q以'\0'标示字符串结束。('\0'q不是普通文本的一部分Q也是'\0'是字W串帔R一部分Q你需要给它分配空_但它q不是字W串内容的一部分Q所以计字W串长度时不会计它Q?br>
1.10外部变量与作用域
p24---(1)函数在用外部变量之前,必须要知道外部变量的名字。要辑ֈ该目的,W一U方式是在函C使用externcd的声明,q种cd的声明除了在前面加了一个关键字extern外,其他斚w与普通变量的声明相同。另一U方式,在源文g中,如果外部变量的定义出现在使用它的函数之前Q那么在那个函数中就没有必要使用extern声明?br>p24---(2)如果E序包含在多个源文g中,而某个变量在file1文g中定义、在file2和file3文g中用,那么在文件file2与file3中就需要用extern声明来徏立该变量与其定义之间的联pR?br>
以下是练习题部分Q?br>1.l习1-9 ~写一个输入复制到输出的程序,q将其中q箋的多个空格用一个空g?br>

2.l习1-11 单的单词计数E序


3.1-19 ~写一个翻转字W串函数


鑫龙 2011-05-03 22:29 发表评论
]]>
רþۺϾĻ| ھƷþþþӰԺ| ŷƷþþ| պþþþĻ| ޹Ʒþþþþ| þŮˬŮˬ| 91Ʒþþþþù۲| þþþƷһ| Ʒþþþþþö| 뾫Ʒþþþ| ޾ƷŮþþ| þùƷþþ| þþž޾Ʒ| Ʒþþþþù91| ˾þþƷ| þùƷ͵99| 99þۺϹƷ| þ˽˹Ʒ| ƷȾþëƬ| þùֱ| þþҹƷ| ĻȾþþþþþ| þþƷƷëƬ | ޾ƷþþþþĻ| þþþ뾫Ʒapp| һþöAV| ھƷþù| 99ƷȾþ| þavСݲ| Ʒպþ| ھƷþþþþ99| þĻƷһ| 97Ʒ˾þô߽| ձѾþþþþþվ| һŮȫƾþƬ| 99þùƷһ| ݺɫþۺ| 2021ھþþƷ| Ʒ99þþþ91gav | պƷþþþþ| þۺɫݺ|