高素质编程师湖南大学语言课件第八章

上传人:桔**** 文档编号:579373843 上传时间:2024-08-26 格式:PPT 页数:83 大小:1.81MB
返回 下载 相关 举报
高素质编程师湖南大学语言课件第八章_第1页
第1页 / 共83页
高素质编程师湖南大学语言课件第八章_第2页
第2页 / 共83页
高素质编程师湖南大学语言课件第八章_第3页
第3页 / 共83页
高素质编程师湖南大学语言课件第八章_第4页
第4页 / 共83页
高素质编程师湖南大学语言课件第八章_第5页
第5页 / 共83页
点击查看更多>>
资源描述

《高素质编程师湖南大学语言课件第八章》由会员分享,可在线阅读,更多相关《高素质编程师湖南大学语言课件第八章(83页珍藏版)》请在金锄头文库上搜索。

1、第八章 指针C程序设计中使用指针可以:l使程序简洁、紧凑、高效l有效地表示复杂的数据结构l动态分配内存l得到多于一个的函数返回值Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.8.1 指针的概

2、念变量与地址程序中: int i; float k; 内存中每个字节有一个编号-地址.2000200120022005内存02003ik 编译或函数调用时为其分配内存单元变量是对程序中数据存储空间的抽象Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyri

3、ght 2004-2011 Aspose Pty Ltd.2000200420062005整型变量i10变量i_pointer200120022003指针与指针变量v指针:一个变量的地址v指针变量:专门存放变量地址的变量叫2000指针指针变量 变量的内容 变量的地址指针变量变量变量地址(指针)变量值指向地址存入指针变量Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created wi

4、th Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.&与*运算符v含义含义: 取变量的地址单目运算符优先级: 2结合性:自右向左含义: 取指针所指向变量的内容单目运算符优先级: 2结合性:自右向左v两者关系:互为逆运算v理解.2000200420062005整型变量i10变量i_pointer2001200220032000指针变量i_pointer-指针变量,它的内容是地址量*i_pointer-指针的目标变量,它的内容是数据&i_pointer-指针变量占用内存的地址

5、200010i_pointer*i_pointer&i_pointerii_pointer &i &(*i_pointer)i *i_pointer *(&i)i_pointer = &i = &(*i_pointer)i = *i_pointer = *(&i)Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.

6、5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.直接访问与间接访问v直接访问:按变量地址存取变量值v间接访问:通过存放变量地址的变量去访问变量例 i=3; -直接访问指针变量.2000200420062005整型变量i10变量i_pointer20012002200320003例 *i_pointer=20; -间接访问20Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 As

7、pose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.指针变量.2000200420062005整型变量i10变量i_pointer2001200220032000整型变量k例 k=i; -直接访问 k=*i_pointer; -间接访问10例 k=i; k=*i_pointer; Evaluation only.Created with Aspose.Slides for .NET 3.5 Cl

8、ient Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.8.2 指针变量指针变量与其所指向的变量之间的关系指针变量的定义v一般形式: 存储类型 数据类型 *指针名;3变量i2000i_pointer*i_pointeri*i_pointer&ii_pointeri=3;*i_pointer=33变量i2000i_p

9、ointer*i_pointeri*i_pointer&ii_pointeri=3;*i_pointer=3合法标识符指针变量本身的存储类型指针的目标变量的数据类型表示定义指针变量不是*运算符例 int *p1,*p2; float *q ; static char *name;注意:1、int *p1, *p2; 与 int *p1, p2;2、指针变量名是p1,p2 ,不是*p1,*p23、指针变量只能指向定义时所规定类型的变量4、指针变量定义后,变量值不确定,应用前必须先赋值Evaluation only.Created with Aspose.Slides for .NET 3.5 C

10、lient Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.指针变量的初始化一般形式:存储类型 数据类型 *指针名=初始地址值;赋给指针变量,不是赋给目标变量例 int i; int *p=&i;变量必须已说明过类型应一致例 int *p=&i; int i;例 int i; int *p=&i; int *q=p;

11、用已初始化指针变量作初值例 main( ) int i; static int *p=&i; . ()不能用auto变量的地址去初始化static型指针Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty

12、Ltd.例 main( ) int i=10; int *p; *p=i; printf(“%d”,*p); 危险!例 main( ) int i=10,k; int *p; p=&k; *p=i; printf(“%d”,*p); 指针变量必须先赋值,再使用.2000200420062005整型变量i10指针变量p200120022003随机Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation on

13、ly.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.零指针与空类型指针v零指针:(空指针)l定义:指针变量值为零l表示: int * p=0; p指向地址为0的单元,系统保证该单元不作它用表示指针变量值没有意义#define NULL 0int *p=NULL:lp=NULL与未对p赋值不同l用途: u避免指针变量的非法引用u在程序中常作为状态比较 例 int *p; . while(p!=NULL) . vvoid *类型指针l表示: void

14、 *p; l使用时要进行强制类型转换例 char *p1; void *p2; p1=(char *)p2; p2=(void *)p1;表示不指定p是指向哪一种类型数据的指针变量Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-20

15、11 Aspose Pty Ltd.例 指针的概念main() int a; int *pa=&a; a=10; printf(a:%dn,a); printf(*pa:%dn,*pa); printf(&a:%x(hex)n,&a); printf(pa:%x(hex)n,pa); printf(&pa:%x(hex)n,&pa);运行结果:a:10*pa:10&a:f86(hex)pa:f86(hex)&pa:f88(hex).f86f8af8cf8b整型变量a10指针变量paf87f88f89f86Evaluation only.Created with Aspose.Slides fo

16、r .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 输入两个数,并使其从大到小输出main() int *p1,*p2,*p,a,b; scanf(%d,%d,&a,&b); p1=&a; p2=&b; if(ab) p=p1; p1=p2; p2=p; printf(a=%d,b

17、=%dn,a,b); printf(max=%d,min=%dn,*p1,*p2);运行结果:a=5,b=9 max=9,min=5.指针变量p1 指针变量p20002008200220042006 指针变量p2 整型变量b 整型变量a5200692008200620082006Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for

18、 .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.指针变量作为函数参数地址传递特点:共享内存,“双向”传递swap(int x,int y) int temp; temp=x; x=y; y=temp;main() int a,b; scanf(%d,%d,&a,&b); if(ab) swap(a,b); printf(n%d,%dn,a,b);例 将数从大到小输出.20002008200A2002200420065变量a 变量b(main)9 变量temp 变量y 变量x(swap)559 59COPYE

19、valuation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.指针变量作为函数参数地址传递特点:共享内存,“双向”传递swap(int x,int y) int temp; temp=x; x=y; y=te

20、mp;main() int a,b; scanf(%d,%d,&a,&b); if(ab) swap(a,b); printf(n%d,%dn,a,b);例 将数从大到小输出值传递.20002008200A2002200420065变量a 变量b(main)9运行结果:5, 9Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for

21、.NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.swap(int *p1, int *p2) int p; p=*p1; *p1=*p2; *p2=p;main() int a,b; int *pointer_1,*pointer_2; scanf(%d,%d,&a,&b); pointer_1=&a; pointer_2=&b; if(ab)swap(pointer_1,pointer_2); printf(n%d,%dn,a,b);.20002008200A200220042006200C200E2010

22、.59整型变量a 整型变量b(main)指针pointer_1指针pointer_220002002(swap)指针p1指针p2整型p5920002002COPY5例 将数从大到小输出Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-

23、2011 Aspose Pty Ltd.swap(int *p1, int *p2) int p; p=*p1; *p1=*p2; *p2=p;main() int a,b; int *pointer_1,*pointer_2; scanf(%d,%d,&a,&b); pointer_1=&a; pointer_2=&b; if(ab)swap(pointer_1,pointer_2); printf(n%d,%dn,a,b);.20002008200A200220042006200C200E2010.59整型变量a 整型变量b(main)指针pointer_1指针pointer_220002

24、00259例 将数从大到小输出运行结果:9,5地址传递Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.swap(int *p1, int *p2) int *p; *p=*p1; *p1=*p

25、2; *p2=*p;main() int a,b; int *pointer_1,*pointer_2; scanf(%d,%d,&a,&b); pointer_1=&a; pointer_2=&b; if(ab) swap(pointer_1,pointer_2); printf(n%d,%dn,a,b);运行结果:9,9编译警告!结果不对!int x;int *p=&x;x;例 将数从大到小输出.20002008200A200220042006200C200E2010.59整型变量a 整型变量b(main)指针pointer_1指针pointer_2200020029920002002CO

26、PY(swap)指针p1指针p2指针p*假设2000指针变量在使用前必须赋值!Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd./*ch9_32.c*/swap(int x,int y) int

27、 t; t=x; x=y; y=t;main() int a,b; int *pointer_1,*pointer_2; scanf(%d,%d,&a,&b); pointer_1=&a; pointer_2=&b; if(ab) swap(*pointer_1,*pointer_2); printf(n%d,%dn,a,b);运行结果:5,9例 将数从大到小输出值传递.20002008200A200220042006200C200E2010.59整型a 整型b(main)pointer_1pointer_2200020029COPY(swap)整型x整型b整型t555 9Evaluation

28、 only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.运行结果:5,9例 将数从大到小输出swap(int *p1, int *p2) int *p; p=p1; p1=p2; p2=p;main() int a,b;

29、 int *pointer_1,*pointer_2; scanf(%d,%d,&a,&b); pointer_1=&a; pointer_2=&b; if(ab) swap(pointer_1,pointer_2); printf(%d,%d,*pointer_1,*pointer_2);.20002008200A200220042006200C200E2010.59整型a 整型b(main)pointer_1pointer_22000200220002002COPY(swap)指针p1指针p2指针p*2000地址传递20002002Evaluation only.Created with

30、Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.8.3 指针与数组指向数组元素的指针变量例 int array10; int *p; p=&array0; / p=array;或 int *p=&array0;或 int *p=array;array

31、0array1array2array3array9.整型指针p&array0p数组名是表示数组首地址的地址常量Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.指针的运算v指针变量的赋值运算lp

32、=&a; (将变量a地址p)lp=array; (将数组array首地址p)lp=&arrayi; (将数组元素地址p)lp1=p2; (指针变量p2值p1)l不能把一个整数p,也不能把p的值整型变量如 int i, *p; p=1000; () i=p; ()指针变量与其指向的变量具有相同数据类型Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.

33、Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.v指针的算术运算:lpi p id (i为整型数,d为p指向的变量所占字节数)lp+, p-, p+i, p-i, p+=i, p-=i等l若p1与p2指向同一数组,p1-p2=两指针间元素个数(p1-p2)/dlp1+p2 无意义例 p指向float数,则 p+1 p+1 4例 p指向int型数组,且p=&a0; 则p+1 指向a1例 int a10; int *p=&a2; p+; *p=1;例 int a10; int *p1=&a2

34、; int *p2=&a5; 则:p2-p1=3;a0a1a2a3a4a5a6a7a8a9a数组pp+1,a+1p+i,a+ip+9,a+91Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.v

35、指针变量的关系运算l若p1和p2指向同一数组,则up1p2 表示p1指的元素在后up1=p2 表示p1与p2指向同一元素l若p1与p2不指向同一数组,比较无意义lp=NULL或p!=NULLEvaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 20

36、04-2011 Aspose Pty Ltd.数组元素表示方法a0a1a2a3a9.aa+9a+1a+2地址元素下标法a0a1a2a9a0a1a2a3a9.pp+9p+1p+2地址元素指针法*p*(p+1)*(p+2)*(p+9) 变址运算符ai *(a+i)ai pi *(p+i) *(a+i)*a*(a+1)*(a+2)*(a+9)p0p1p2p9Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluatio

37、n only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.a0a1a2a3a4例 数组元素的引用方法main() int a5,*pa,i; for(i=0;i5;i+) ai=i+1; pa=a; for(i=0;i5;i+) printf(*(pa+%d):%dn,i,*(pa+i); for(i=0;i5;i+) printf(*(a+%d):%dn,i,*(a+i); for(i=0;i5;i+) printf(pa%d:%dn,i,p

38、ai); for(i=0;i5;i+) printf(a%d:%dn,i,ai);12345paEvaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 int a=1,2,3,4,5,6,7,8,

39、9,10,*p=a,i; 数组元素地址的正确表示:(A)&(a+1) (B)a+ (C)&p (D)&pi数组名是地址常量p+,p- ()a+,a- ()a+1, *(a+2) ()Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2

40、011 Aspose Pty Ltd.例 void main() int a =5,8,7,6,2,7,3; int y,*p=&a1; y=(*-p)+; printf(“%d ”,y); printf(“%d”,a0); 输出:5 6pp58762730123456a例 注意指针变量的运算6Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Sl

41、ides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.main() int i,*p,a7; p=a; for(i=0;i7;i+) scanf(%d,p+); printf(n); for(i=0;i7;i+,p+) printf(%d,*p);例 注意指针的当前值p=a;pp58762730123456apppppp指针变量可以指到数组后的内存单元Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2

42、.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.数组名作函数参数v数组名作函数参数,是地址传递v数组名作函数参数,实参与形参的对应关系实参形参数组名指针变量数组名指针变量数组名数组名指针变量指针变量Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile

43、5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 将数组a中的n个整数按相反顺序存放 ij 3 7 9 11 0 6 7 5 4 20 1 2 3 4 5 6 7 8 9ijijijji11760594723实参与形参均用数组void inv(int x, int n) int t,i,j,m=(n-1)/2; for(i=0;i

44、=m;i+) j=n-1-i; t=xi; xi=xj; xj=t; main() int i,a10=3,7,9,11,0,6,7,5,4,2; inv(a,10); printf(The array has been reverted:n); for(i=0;i10;i+) printf(%d,ai); printf(n);m=4Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Cr

45、eated with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 将数组a中的n个整数按相反顺序存放 void inv(int *x, int n) int t,*p,*i,*j,m=(n-1)/2; i=x; j=x+n-1; p=x+m; for(;i=p;i+,j-) t=*i; *i=*j; *j=t; main() int i,a10=3,7,9,11,0,6,7,5,4,2; inv(a,10); printf(The array has been re

46、verted:n); for(i=0;i10;i+) printf(%d,ai); printf(n);实参用数组,形参用指针变量37911067542a0a1a2a3a4a5a6a7a8a9xp=x+ma数组60711594723ijijijjijiEvaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Clie

47、nt Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 将数组a中的n个整数按相反顺序存放 void inv(int *x, int n) int t,*i,*j,*p,m=(n-1)/2; i=x; j=x+n-1; p=x+m; for(;i=p;i+,j-) t=*i; *i=*j; *j=t; main() int i,a10,*p=a; for(i=0;i10;i+,p+) scanf(%d,p); p=a; inv(p,10); printf(The array has been reverted:n); for(p=a;pa

48、+10;p+) printf(%d,*p);实参与形参均用指针变量Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 将数组a中的n个整数按相反顺序存放 void inv(int x, int

49、 n) int t,i,j,m=(n-1)/2; for(i=0;i=m;i+) j=n-1-i; t=xi; xi=xj; xj=t; main() int i,a10,*p=a; for(i=0;i10;i+,p+) scanf(%d,p); p=a; inv(p,10); printf(The array has been reverted:n); for(p=arr;parr+10;p+) printf(%d ,*p);实参用指针变量,形参用数组Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile

50、5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.v一级指针变量与一维数组的关系int *p 与 int q10 l数组名是指针(地址)常量lp=q; p+i 是qi的地址l数组元素的表示方法:下标法和指针法, 即若p=q, 则 pi qi *(p+i) *(q+i) l形参数组实质上是指针变量,即int q int *ql在定义指针

51、变量(不是形参)时,不能把int *p 写成int p;l系统只给p分配能保存一个指针值的内存区(一般2字节);而给q分配2*10字节的内存区Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.指

52、针与二维数组v二维数组的地址对于一维数组:(1)数组名array表示数组的首地址,即array0的地址;(2)数组名array是地址常量(3)array+i是元素arrayi的地址(4)arrayi *(array+i)arrayint array10;Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Cli

53、ent Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.对于二维数组:(1)a是数组名, 包含三个元素 a0,a1,a2(2)每个元素ai 又是一个一维 数组,包含4个 元素aa+1a+2*(*(a+0)+1)*(a0+1)int a34;a0a1a2200020082016200020022008201020162018a00a01a10a11a20a21a02a03a12a13a22a23基类型行指针与列指针a0+1a1+1a2+1*(a+0)+1*(a+1)+1*(a+2)+1Evaluation only.Created with

54、Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.l对二维数组 int a34,有ua-二维数组的首地址,即第0行的首地址ua+i-第i行的首地址uai *(a+i)-第i行第0列的元素地址uai+j *(a+i)+j -第i行第j列的元素地址u*(a

55、i+j) *(*(a+i)+j) aijla+i=&ai=ai=*(a+i) =&ai0, 值相等,含义不同ua+i &ai,表示第i行首地址,指向行uai *(a+i) &ai0,表示第i行第0列元素地址,指向列int a34;a0a1a2200020082016200020022008201020162018a00a01a10a11a20a21a02a03a12a13a22a23aa+1a+2Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 A

56、spose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.int a34;a00a01a10a11a20a21a02a03a12a13a22a23二维数组元素表示形式:(1)a12(2)*(a1+2)(3)*(*(a+1)+2)(4)*(&a00+1*4+2)地址表示:(1) a+1 (2) &a10(3) a1(4) *(a+1)(5)(int *) (a+1)行指针列指针地址表示:(1) &a1

57、2(2) a1+2(3) *(a+1)+2(4)&a00+1*4+2Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.表示形式含义地址a二维数组名,数组首地址a0,*(a+0),*a第0行第0列

58、元素地址a+1第1行首地址a1,*(a+1)第1行第0列元素地址a1+2,*(a+1)+2,&a12第1行第2列元素地址*(a1+2),*(*(a+1)+2),a12第1行第2列元素值2000200020082008201213Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5

59、.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.v二维数组的指针变量l指向二维数组元素的指针变量例 指向二维数组元素的指针变量main() static int a34=1,3,5,7,9,11,13,15,17,19,21,23; int *p; for(p=a0;pa0+12;p+) if(p-a0)%4=0) printf(n);printf(%4d ,*p); p=*a; p=&a00; p=*(a+0); p=a; p=*a; p=&a00; p=(int *)a; p=a; int a34;a00a01a10a11a20a21a02a03a12a

60、13a22a23pEvaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.l指向一维数组的指针变量u定义形式: 数据类型 (*指针名)一维数组维数; 例 int (*p)4;( )不能少int (*p

61、)4与int *p4不同p的值是一维数组的首地址,p是行指针u可让p指向二维数组某一行 如 int a34, (*p)4=a;int a34;a00a01a10a11a20a21a02a03a12a13a22a23aa+1a+2pp+1p+2p0+1或 *p+1p1+2或 *(p+1)+2*(*p+1)或 (*p)1 *(*(p+1)+2)一维数组指针变量维数和二维数组列数必须相同Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pt

62、y Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 一维数组指针变量举例main() static int a34=1,3,5,7,9,11,13,15,17,19,21,23; int i,j,(*p)4; for(p=a,i=0;i3;i+,p+) for(j=0;j4;j+) printf(%d ,*(*p+j); printf(n);p=a0; p=*a; p=&a00; p=&a0; p=a0;

63、 p=*a; p=&a00; p=&a0; int a34;a00a01a10a11a20a21a02a03a12a13a22a23ppp p0jEvaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd

64、.例 二维数组与指针运算main() int a34=1,2,3,4,3,4,5,6,5,6,7,8; int i; int (*p)4=a,*q=a0; for(i=0;i3;i+) if(i=0) (*p)i+i/2=*q+1;else p+,+q; for(i=0;i3;i+) printf(%d,aii); printf(%d,%dn,*(int *)p),*q);运行结果:2,4,7,5,3123434565678pq2pqpqEvaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.

65、Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.v二维数组的指针作函数参数l用指向变量的指针变量l用指向一维数组的指针变量l用二维数组名实参形参数组名int x4指针变量int (*q)4数组名int x4指针变量int (*q)4数组名a数组名a指针变量p1指针变量p1若int a34; int (*p1)4=a; int *p2=a0;指针变量

66、p2指针变量int *qEvaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 3个学生各学4门课,计算总平均分,并输出第n个学生成绩main() void average(float *p,in

67、t n); void search(float (*p)4,int n); float score34=65,67,79,60,80,87,90,81,90,99,100,98; average(*score,12); search(score,2);void average(float *p,int n) float *p_end, sum=0,aver; p_end=p+n-1; for(;p=p_end;p+)sum=sum+(*p); aver=sum/n; printf(average=%5.2fn,aver);void search(float (*p)4, int n) int

68、i; printf( No.%d :n,n); for(i=0;i4;i+) printf(%5.2f ,*(*(p+n)+i);列指针行指针函数说明float p46552796080879081909910098pp pniEvaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5

69、.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 3个学生各学4门课,计算总平均分,并查找一门以上课 不及格学生, 输出其各门课成绩void search(float (*p)4, int n) int i,j,flag; for(j=0;jn;j+) flag=0;for(i=0;i4;i+) if(*(*(p+j)+i)60) flag=1;if(flag=1) printf(No.%d is fail,his scores are:n,j+1); for(i=0;iy) z=x; else z=y; return(z);main() int max(

70、int ,int), (*p)(); int a,b,c; p=max; scanf(%d,%d,&a,&b); c=(*p)(a,b); printf(a=%d,b=%d,max=%dn,a,b,c);int max(int x,int y) int z; if(xy) z=x; else z=y; return(z);Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created

71、with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.用函数指针变量作函数参数例 用函数指针变量作参数,求最大值、最小值和两数之和void main() int a,b,max(int,int), min(int,int),add(int,int); void process(int,int,int (*fun)(); scanf(%d,%d,&a,&b); process(a,b,max); process(a,b,min); process(a,b,add);voi

72、d process(int x,int y,int (*fun)() int result; result=(*fun)(x,y); printf(%dn,result);max(int x,int y) printf(“max=”);printf(“max=”); return(xy?x:y); return(xy?x:y);min(int x,int y) printf(“min=”);printf(“min=”); return(xy?x:y); return(xy?x:y);add(int x,int y) printf(“sum=”); printf(“sum=”); return(

73、x+y); return(x+y);Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.8.6 返回指针值的函数 函数定义形式: 类型标识符 *函数名(参数表);例 int *f(int x, i

74、nt y)例 指针函数实现:有若干学生成绩,要求输入学生序号后, 能输出其全部成绩main() float score4=60,70,80,90, 56,89,67,88,34,78,90,66; float *search(float (*pointer)4,int n), *p; int i,m; printf(Enter the number of student:); scanf(%d,&m); printf(The scores of No.%d are:n,m); p=search(score,m); for(i=0;i*y)return x; elsereturn y;main(

75、) int a=2,b=3; int *p; p=f1(&a, &b); printf(%dn,*p);.20002008200A20022004200623 指针变量y 指针变量x(f1)20022000COPY变量a 变量b(main) 指针变量p*Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Cli

76、ent Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 写一个函数,求两个int型变量中居于较大值的变量的地址.20002008200A2002200420062变量a 变量b(main)3 指针变量p*2002int *f3(int *x,int *y) if(*x*y)return x; elsereturn y;main() int a=2,b=3; int *p; p=f1(&a,&b); printf(%dn,*p);Evaluation only.Created with Aspose.Slides for .NET 3.5

77、 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 写一个函数,求两个int型变量中居于较大值的变量的地址int *f3(int x,int y) if(xy)return &x; elsereturn &y;main() int a=2,b=3; int *p; p=f3(a, b); printf

78、(%dn,*p);.20002008200A20022004200623 变量y 变量x(f3)32COPY变量a 变量b(main) 指针变量p*Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Lt

79、d.例 写一个函数,求两个int型变量中居于较大值的变量的地址不能返回形参或局部变量的地址作函数返回值.20002008200A2002200420062变量a 变量b(main)3 指针变量p*200Aint *f3(int x,int y) if(xy)return &x; elsereturn &y;main() int a=2,b=3; int *p; p=f3(a,b); printf(%dn,*p);Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 200

80、4-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.8.7 指针数组和多级指针用于处理二维数组或多个字符串指针数组v定义:数组中的元素为指针变量v定义形式:存储类型 数据类型 *数组名数组长度说明;例 int *p4;指针所指向变量的数据类型指针本身的存储类型区分int *p4与int (*p)4v指针数组赋值与初始化赋值:main() int b23,*pb2; pb0=b0

81、; pb1=b1; .int *pb2pb0pb1int b23123246初始化:main() int b23,*pb =b0,b1; .int *pb2pb0pb1int b23123246Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright

82、2004-2011 Aspose Pty Ltd.v指针数组赋值与初始化L i s p 0F o r t r a n 0B a s i c 0p0p1p2p30赋值:main() char a=Fortran; char b=Lisp; char c=Basic; char *p4; p0=a; p1=b; p2=c; p3=NULL; .或:main() char *p4; p0= Fortran; p1= Lisp; p2= Basic; p3=NULL; .初始化:main() char *p=Fortran, Lisp, Basic,NULL; .L i s p 0F o r t r

83、a n 0B a s i c 0p0p1p2p30Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd. char name59=“gain”,“much”,“stronger”, “point”,

84、“bye”; char *name5=“gain”,“much”,“stronger”, “point”,“bye”;g a i n 0s t r o n g e r 0p o i n t 0m u c h 0name0name1name2name3name4b y e 0g a i n 0s t r o n g e r 0p o i n t 0m u c h 0b y e 0v二维数组与指针数组区别:二维数组存储空间固定字符指针数组相当于可变列长的二维数组分配内存单元=数组维数*2+各字符串长度指针数组元素的作用相当于二维数组的行名但指针数组中元素是指针变量二维数组的行名是地址常量Evalu

85、ation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.main() int b23,*pb2; int i,j; for(i=0;i2;i+) for(j=0;j3;j+) bij=(i+1)*(j+1);

86、pb0=b0; pb1=b1; for(i=0;i2;i+) for(j=0;j3;j+,pbi+) printf(b%d%d:%2dn,i,j,*pbi);例 用指针数组处理二维数组int *pb2pb0pb1int b23b00 *pb0b01 *(pb0+1)b02 *(pb0+2)b10 *pb1b11 *(pb1+1)b12 *(pb1+2)123246Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Eva

87、luation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 对字符串排序(简单选择排序)main() void sort(char *name,int n), print(char *name,int n); char *name=Follow me,BASIC, Great Wall,FORTRAN,Computer ; int n=5; sort(name,n); print(name,n);void sort(char *n

88、ame,int n) char *temp; int i,j,k; for(i=0;in-1;i+) k=i; for(j=i+1;j0) k=j; if(k!=i) temp=namei; namei=namek; namek=temp; name0name1name2name3name4nameGreat WallFORTRANComputerFollow meBASICkjkjjji=0Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Asp

89、ose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 对字符串排序(简单选择排序)main() void sort(char *name,int n), print(char *name,int n); char *name=Follow me,BASIC, Great Wall,FORTRAN,Computer ; int n=5; sort(name,n); print(name,n);vo

90、id sort(char *name,int n) char *temp; int i,j,k; for(i=0;in-1;i+) k=i; for(j=i+1;j0) k=j; if(k!=i) temp=namei; namei=namek; namek=temp; name0name1name2name3name4nameGreat WallFORTRANComputerFollow meBASICkkjjji=1kEvaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyrigh

91、t 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 对字符串排序(简单选择排序)main() void sort(char *name,int n), print(char *name,int n); char *name=Follow me,BASIC, Great Wall,FORTRAN,Computer ; int n=5; sort(name,n); p

92、rint(name,n);void sort(char *name,int n) char *temp; int i,j,k; for(i=0;in-1;i+) k=i; for(j=i+1;j0) k=j; if(k!=i) temp=namei; namei=namek; namek=temp; name0name1name2name3name4nameGreat WallFORTRANComputerFollow meBASICkkjji=2Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2

93、.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 对字符串排序(简单选择排序)main() void sort(char *name,int n), print(char *name,int n); char *name=Follow me,BASIC, Great Wall,FORTRAN,Computer ; int n=5; so

94、rt(name,n); print(name,n);void sort(char *name,int n) char *temp; int i,j,k; for(i=0;in-1;i+) k=i; for(j=i+1;j0) k=j; if(k!=i) temp=namei; namei=namek; namek=temp; name0name1name2name3name4nameGreat WallFORTRANComputerFollow meBASICkkji=3Evaluation only.Created with Aspose.Slides for .NET 3.5 Client

95、 Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 对字符串排序(简单选择排序)main() void sort(char *name,int n), print(char *name,int n); char *name=Follow me,BASIC, Great Wall,FORTRAN,Computer ;

96、 int n=5; sort(name,n); print(name,n);void sort(char *name,int n) char *temp; int i,j,k; for(i=0;in-1;i+) k=i; for(j=i+1;j0) k=j; if(k!=i) temp=namei; namei=namek; namek=temp; name0name1name2name3name4nameGreat WallFORTRANComputerFollow meBASICEvaluation only.Created with Aspose.Slides for .NET 3.5

97、Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.多级指针v定义: 指向指针的指针v一级指针:指针变量中存放目标变量的地址p1&p2&i3P2(指针变量)i(整型变量)例 int *p1; int *p2; int i=3; p2=&i; p1=&p2; *p1=5;v二级指针:指针变量中存放一级指针变量

98、的地址例 int *p; int i=3; p=&i; *p=5;&i3P(指针变量)i(整型变量)一级指针单级间接寻址二级指针一级指针目标变量二级间接寻址Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pt

99、y Ltd.l定义形式:存储类型 数据类型 *指针名;如 char *p;例 int i, *p; p=&i; ()/p是二级指针,不能用变量地址为其赋值指针本身的存储类型最终目标变量的数据类型*p是p间接指向对象的地址*p是p间接指向对象的值例 int i=3; int *p1; int *p2; p1=&i; p2=&p1; *p=5;ip1p23&i&p1*p2, *p1*p2v多级指针例 三级指针 int *p; 四级指针 char *p;Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2

100、.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.20002008200A20022004200612变量a 变量b(main) 指针变量p2000 指针变量q2002例 一级指针与二级指针#include void swap(int *r,int *s) int *t; t=r; r=s; s=t;main() int a=1,b=2,*

101、p,*q; p=&a; q=&b; swap(p,q); printf(%d,%dn,*p,*q);20022000COPY 指针变量s 指针变量r(swap) 指针变量t200020022000Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright

102、 2004-2011 Aspose Pty Ltd.20002008200A20022004200612变量a 变量b(main) 指针变量p2000 指针变量q2002例 一级指针与二级指针#include void swap(int *r,int *s) int *t; t=r; r=s; s=t;main() int a=1,b=2,*p,*q; p=&a; q=&b; swap(p,q); printf(%d,%dn,*p,*q);输出: 1,2Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile

103、5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 一级指针与二级指针#include void swap(int *r,int *s) int *t; t=r; r=s; s=t;main() int a=1,b=2,*p,*q; p=&a; q=&b; swap(p,q); printf(%d,%dn,*p,*q);abpqab

104、pqrsabpqsrabpq输出: 1,2Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 一级指针与二级指针#include void swap(int *r,int *s) int *t

105、; t=*r; *r=*s; *s=t;main() int a=1,b=2,*p,*q; p=&a; q=&b; swap(&p,&q); printf(%d,%dn,*p,*q);20002008200A20022004200612变量a 变量b(main) 指针变量p2000 指针变量q200220062004COPY 二级指针s 二级指针r(swap) 指针变量t200020022000Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 As

106、pose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 一级指针与二级指针#include void swap(int *r,int *s) int *t; t=*r; *r=*s; *s=t;main() int a=1,b=2,*p,*q; p=&a; q=&b; swap(&p,&q); printf(%d,%dn,*p,*q);20002008200A20022004200612变量a

107、变量b(main) 指针变量p2000 指针变量q200220002002输出: 2,1Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 一级指针与二级指针#include void swa

108、p(int *r,int *s) int *t; t=*r; *r=*s; *s=t;main() int a=1,b=2,*p,*q; p=&a; q=&b; swap(&p,&q); printf(%d,%dn,*p,*q);abpqbapqabrspqabrspq输出: 2,1Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides f

109、or .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 用二级指针处理字符串#define NULL 0void main() char *p; char *name=hello,good,world,bye,; p=name+1; printf(%o : %s , *p,*p); p+=2; while(*p!=NULL) printf(%sn,*p+);name0name1name2name3name4char *name5worldbye0hellogoodnamep运行结果:644 : good b

110、ye用*p可输出地址(%o或%x), 也可用它输出字符串(%s)p*(p+)Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.v二级指针与指针数组的关系int *p 与 int *q10 l指针

111、数组名是二级指针常量lp=q; p+i 是qi的地址l指针数组作形参,int *q 与int *q完全等价;但作为变量定义两者不同l系统只给p分配能保存一个指针值的内存区;而给q分配10块内存区,每块可保存一个指针值Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0

112、.Copyright 2004-2011 Aspose Pty Ltd.命令行参数v命令行:在操作系统状态下,为执行某个程序而键入的一行字符v命令行一般形式:命令名 参数1 参数2参数nmain(int argc, char *argv) v命令行参数传递v带参数的main函数形式:C:TC copy.exe source.c temp.c有3个字符串参数的命令行命令行中参数个数元素指向命令行参数中各字符串首地址形参名任意命令行实参main(形参)系统自动调用main函数时传递第一个参数: main所在的可执行文件名可执行文件名Evaluation only.Created with Aspo

113、se.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.例 输出命令行参数/*test.c*/main(int argc, char *argv) while(argc1) +argv; printf(%sn,*argv); -argc; main(int argc

114、, char *argv) while(argc-0) printf(%sn,*argv+);1. 编译、链接test.c,生成可执行文件test.exe2. 在DOS状态下运行(test.exe所在路径下)例如: C:TC test.exe hello world!运行结果:hello world!运行结果:test hello world!argv0argv1argv2char *argvworldtesthelloargvargc=3Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0

115、.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.定义含义int i;int *p;int an;int *pn;int (*p)n;int f();int *p();int (*p)();int *p;定义整型变量ip为指向整型数据的指针变量定义含n个元素的整型数组an个指向整型数据的指针变量组成的指针数组pp为指向含n个元素的一维整型数组的指针

116、变量f为返回整型数的函数p为返回指针的函数,该指针指向一个整型数据p为指向函数的指针变量,该函数返回整型数p为指针变量,它指向一个指向整型数据的指针变量指针的数据类型Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspo

117、se Pty Ltd.例 下列定义的含义(1)int *p3;(2)int (*p)3;(3)int *p(int);(4)int (*p)(int);(5)int *(*p)(int);(6)int (*p3)(int);(7)int *(*p3)(int);函数指针数组,函数返回int型指针指针数组指向一维数组的指针返回指针的函数指向函数的指针,函数返回int型变量指向函数的指针,函数返回int 型指针函数指针数组,函数返回int型变量Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.Evaluation only.Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.Copyright 2004-2011 Aspose Pty Ltd.

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 资格认证/考试 > 自考

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号