C数组指针与字符串PPT课件_第1页
C数组指针与字符串PPT课件_第2页
C数组指针与字符串PPT课件_第3页
C数组指针与字符串PPT课件_第4页
C数组指针与字符串PPT课件_第5页
已阅读5页,还剩88页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 第1页/共93页数组是具有一定顺序关系的若干相同类型变量的集合体,组成数组的变量称为该数组的元素。数组属于构造类型。第2页/共93页一维数组的声明类型说明符 数组名 常量表达式 ;例如: int a10; 表示 a 为整型数组,有10个元素:a0.a9引用:必须先声明,后使用。只能逐个引用数组元素,而不能一次引用整个数组!例如:a0=a5+a7-a2*3第3页/共93页/ 数组元素的使用#include void main() double a3; a0 =; a1 =; a2 =; cout a0 = a0 endl; cout a1 = a1 endl; cout a2 = a2 end

2、l; cout a0 + a1 + a2 = a0 + a1 + a2 endl;/* 程序运行结果 * 程序结束 */第4页/共93页数组元素在内存中顺次存放,它们的地址是连续的。例如:具有10个元素的数组 a,在内存中的存放次序如下:数组名字是数组首元素的内存地址。数组名是一个常量,不能被赋值。a0 a1a2 a3 a4a5 a6 a7a8 a9a第5页/共93页第6页/共93页/ #include void main() const int SIZE=5; double aSIZE; int i; cout Enter SIZE numbers: ; for (i = 0; I ai;

3、cout = 0; i -) cout ai ; cout endl;/* 程序运行结果 *Enter 5 numbers: 1 3 5 7 9In reverse order: 9 7 5 3 1* 程序结束 */第7页/共93页可以在编译阶段使数组得到初值:在声明数组时对数组元素赋以初值。例如:static int a10 = 0,1,2,3,4,5,6,7,8,9 ;可以只给一部分元素赋初值。例如:static int a10 = 0,1,2,3,4 ; 在对全部数组元素赋初值时,可以不指定数组长度。例如:static int a = 1,2,3,4,5 ;第8页/共93页/ 一维数组的

4、初始化#include void main() float a =, 66.6 ; int size = sizeof(a)/sizeof(float); for (int i=0; isize; i+) cout a i = ai endl;/* 程序运行结果 * 程序结束 */第9页/共93页/ Causing an unhandled exception#include void main() double a =, 66.6 ; double x=; cout x = x endl; a3333 =; / ERROR ! cout x = x endl; cout a endl;第10

5、页/共93页/* 程序运行结果 *第11页/共93页第12页/共93页/用数组来处理求Fibonacci数列,输出前20项。#include void main() int i ; static int f20 = 1 , 1 ; /初始化第0、1个数 for(i = 2; i 20; i +) fi = fi-2 + fi-1 ; /求第219个数 for(i = 1; i = 20; i +) /输出,每行5个数 cout fi-1 t ; if(i%5=0) cout endl ; 第13页/共93页二维数组的声明类型说明符 数组名常量表达式常量表达式例如:float a34;引用例如:

6、b12=a23/2存储顺序:按行存放第14页/共93页分行给二维数组赋初值static int a34=1,2,3,4,5,6,7,8,9,10,11,12;将所有数据写在一个内,按顺序赋值static int a34=1,2,3,4,5,6,7,8,9,10,11,12;可以对部分元素赋初值例如:static int a34=1,0,6,0,0,11;第15页/共93页数组元素作实参,与单个变量一样。数组名作参数,形、实参数都应是数组名,类型要一样,。第16页/共93页第17页/共93页/ Passing an array to a function#include void read(in

7、t a , int& n);void print(int a , int n);int sum(int a , int n);void main( ) const int MAXSIZE=100; int aMAXSIZE=0, size; read(a,size); cout 数组中有 size 个元素: ; print(a,size); cout 数组中各元素的和为 sum(a,size) endl;/* 程序运行结果 *输入整数(输入0, 则结束!):a0: 2a1: 4a2: 6a3: 0数组中有 3 个元素: 2 4 6数组中各元素的和为 12* 程序结束 */第18页/共9

8、3页void read(int a , int& n) cout 输入整数(输入0, 则结束!):n; n = 0; do cout a n an; while (an+ != 0); -n; / dont count the 0void print(int a , int n) for (int i=0; in; i+) cout ai ; cout endl;int sum(int a , int n) int sum=0; for (int i=0; in; i+) sum += ai; return sum;第19页/共93页第20页/共93页#include void Row

9、Sum(int A 4, int nrow) for (int i = 0; i nrow; i+) int sum = 0; for(int j = 0; j 4; j+) sum += Aij; cout Sum of row i is sum endl; void main(void) int Table34 = 1,2,3,4,2,3,4,5,3,4,5,6; for (int i = 0; i 3; i+) for (int j = 0; j 4; j+) cout Tableij ; cout endl; RowSum(Table,3);/* 程序运行结果 *1 2 3 42 3

10、4 53 4 5 6Sum of row 0 is 10Sum of row 1 is 14Sum of row 2 is 18* 程序结束 */第21页/共93页定义:类名 数组名元素个数;访问方法:通过下标访问 数组名下标.成员名第22页/共93页 数组中每一个元素对象被创建时,系统都会调用构造函数类初始化该对象。通过初始化列表赋值。例: Location A2 = Location(1,2),Location(3,4);如果没有为数组元素指定显式初始值,数组元素使用缺省值初始化(调用缺省构造函数)。Location B2 = Location(5,8);Location C2;第23页/

11、共93页不定义构造函数,则采用缺省构造函数。各元素对象的初值要求为相同的值时,可以定义出具有缺省形参值的构造函数。各元素对象的初值要求为不同的值时,需要定义带形参(无缺省值)的构造函数。当数组中每一个对象被删除时,系统都要调用一次析构函数。第24页/共93页/#if ! defined(_LOCATION_H)#define _LOCATION_Hclass Location public: Location( ); Location(int xx,int yy); Location( ); void Move(int x,int y); int GetX() return X; int Ge

12、tY() return Y; private: int X,Y;#endif#include#include Location.hLocation:Location() X = Y = 0; cout Default Constructor called. endl;Location:Location(int xx, int yy) X = xx; Y = yy; cout Constructor called. endl;Location:Location() cout Destructor called. endl;void Location:Move(int x, int y) X =

13、x; Y = y;第25页/共93页#include#include Location.hint main() coutEntering main.endl; Location A2; for(int i=0;i2;i+) Ai.Move(i+10,i+20); coutExiting main.endl; return 0;/* 程序运行结果 *Entering main.Default Constructor called.Default Constructor called.Exiting main.Destructor called.Destructor called.* 程序结束 *

14、/第26页/共93页指针:另一个变量的地址, 用于间接访问变量指针变量:用于存放地址的变量指针运算符:*取地址运算符:&声明int i;int *i_pointer=&i; / 指向整型变量i的指针引用例1: i=3;例2: *i_pointer=3;第27页/共93页语法形式 存储类型 数据类型 *指针名初始地址;例: int *pa=&a;注意事项 用变量地址作为初值时,该变量必须在指针初始化之前已说明过,且变量类型应与指针类型一致。 可以用一个已赋初值的指针去初始化另一 个指针变量。 不要用一个内部 auto 型变量去初始化 static 型指针。第28页/共93

15、页第29页/共93页/由书P163. 例6-5改编#includevoid main() int *p ; int i ; / int *p , i ; p = & i ; i=10; cout i = i endl ; cout & i = & i endl ; cout * p = * p endl ; cout p = p endl ; cout & p = & p endl ;/* 程序运行结果 * i = 10 & i = 0 x0012FF78 * p = 10 p = 0 x0012FF78 & p = 0 x0012FF7

16、C* 程序结束 */第30页/共93页指针与整数的加减运算 指针 p 加上或减去 n ,其意义是指针当前指向位置的前方或后方第 n 个数据的地址。 运算的结果值取决于指针指向的数据类型。指针加一,减一运算 指向下一个或前一个数据。例如:y=*px+ 相当于 y=*(px+) (*和+优先级相同,自右向左运算) 第31页/共93页papa-2pa-1pa+1pa+2pa+3*(pa-2)*pa*(pa+1)*(pa+2)*(pa+3)*(pa-1)short *pa第32页/共93页pb-1pbpb+1pb+2*(pb-1)*pb*(pb+1)*(pb+2)long *pb第33页/共93页关系

17、运算 指向相同类型数据的指针之间可以进行各种关系运算。 指向不同数据类型的指针,以及指针与一般整数变量之间的关系运算是无意义的。指针可以和零之间进行等于或不等于的关系运算。例如:p=0或p!=0第34页/共93页指针名=地址“地址”中存放的数据类型与指针类型必须相符。向指针变量赋的值必须是地址常量或变量,不能是普通整数。但可以赋值为整数0,表示空指针。 允许定义指向 void 类型的指针。该指针可以被赋予任何类型对象的地址。例: void *general; 第35页/共93页定义与赋值例: int a10, *pa; pa=&a0; 或 pa=a;通过指针引用数组元素经过上述定义及赋

18、值后:*pa就是a0,*(pa+1)就是a1,*(pa+i)就是ai.ai, *(pa+i), *(a+i), pai都是等效的。不能写 a+,因为a是数组首地址,是。第36页/共93页第37页/共93页#include void main() const int SIZE = 3; int aSIZE = 55, 66, 77 ; cout a = a endl; cout sizeof(int) = sizeof(int) endl; int * end = a + SIZE; / converts SIZE to offset 6 int sum = 0; for ( int * p =

19、 a; p end; p +) sum += *p; cout p = p; cout *p = *p; cout sum = sum endl; cout end = end endl;/* 程序运行结果 *a = 0 x0012FF70sizeof(int) = 4 p = 0 x0012FF70 *p = 55 sum = 55 p = 0 x0012FF74 *p = 66 sum = 121 p = 0 x0012FF78 *p = 77 sum = 198end = 0 x0012FF7C* 程序结束 */第38页/共93页第39页/共93页#include void main()

20、 int a = 22, 33, 44, 55, 66; int *p; cout a = a , *a = *a endl; for (p = a; p a + 5; p+) cout p = p , *p = *p endl;/* 程序运行结果 *a = 0 x0012FF6C, *a = 22p = 0 x0012FF6C, *p = 22p = 0 x0012FF70, *p = 33p = 0 x0012FF74, *p = 44p = 0 x0012FF78, *p = 55p = 0 x0012FF7C, *p = 66* 程序结束 */第40页/共93页第41页/共93页/使用

21、数组下标void main( ) int a10 ; int i ; for ( i = 0 ; i ai ; cout endl ; for ( i = 0 ; i10 ; i + ) cout ai ;第42页/共93页/使用数组名void main( ) int a10 ; int i ; for ( i = 0 ; i ai ; cout endl ; for ( i = 0 ; i 10 ; i +)cout * ( a + i ) ;第43页/共93页/使用指针变量void main( ) int a10 ; int *p , i ; for ( i = 0 ; i ai ; co

22、ut endl ; for ( p = a ; p ( a + 10 ) ; p + ) cout * p ;第44页/共93页第45页/共93页#include void swap(int *p1, int *p2)int p;p=*p1; *p1=*p2; *p2=p; void main( )int a,b;int *p1, *p2; cin a b;p1 = & a; p2 = & b; if ( a b ) swap ( p1 , p2 ); cout a = a tb = b endl;/* 程序运行结果 *22 99a = 99 b = 22* 程序结束 */第4

23、6页/共93页第47页/共93页#include void invert(int *p ,int n) /实参是数组名时形参可以是指针。 int temp, i , j , m = (n-1)/2; for(i=0;i=m;i+) j=n-1-i; /temp = *(p+i);*(p+i)=*(p+j);*(p+j)=temp; temp = pi;pi=pj; pj=temp; void main( ) int i, a6=3,7,9,2,0,6; cout 原 数 组: ; for(i=0;i6;i+) cout ai ;cout endl; invert(a,6); cout 反置后数

24、组: ; for(i=0;i6;i+) cout ai ;cout endl;/* 程序运行结果 *原 数 组: 3 7 9 2 0 6反置后数组: 6 0 2 9 7 3* 程序结束 */第48页/共93页数组的元素是指针型例:Location *pa2;/由pa0,pa1两个指针组成 第49页/共93页第50页/共93页#include void main( ) int line1 =1,0,0;/声明数组,矩阵的第一行int line2 =0,1,0;/声明数组,矩阵的第二行int line3 =0,0,1;/声明数组,矩阵的第三行int *p_line3;/声明整型指针数组p_line

25、0=line1;/初始化指针数组元素p_line1=line2;p_line2=line3;coutMatrix test:endl;for(int i=0;i3;i+)for( int j = 0 ; j 3 ; j + ) cout p_lineij ; coutendl;/* 程序运行结果 *Matrix test:1 0 00 1 00 0 1* 程序结束 */第51页/共93页指向常量的指针 不能通过指针来改变所指对象的值,但指针本身可以改变,可以指向另外的对象。例: const char *name1 = John; 若定义指针常量,则指针本身的值不能被改变。例: char *co

26、nst name2 = John; 第52页/共93页#include int main() int *pt_int; float *pt_float; int pig = 7, dog = 27; float x =, y =; void *general; pt_int = &pig; *pt_int += dog; cout Pig now has the value of *pt_int n; general = pt_int; /指向int型的指针赋值给void型指针 pt_float = &x; y += 5 * (*pt_float); cout y now ha

27、s the value of y n; general = pt_float; /指向float型的指针赋值给void型指针 const char *name1 = John; /指向常量的指针,所指对象之值不能改变 char *const name2 = John; /常量指针,指针本身不能被改变 return 0;/* 程序运行结果 *Pig now has the value of 34* 程序结束 */第53页/共93页第54页/共93页#includeconst int N=6;void print(const int *p,int n);void main() int arrayN

28、; for(int i=0;iarrayi; print(array,N); void print(const int *p, int n) cout*p; for(int i=1;in;i+) cout.*(p+i); coutendl;/* 程序运行结果 *11 22 33 44 55 6611.22.33.44.55.66* 程序结束 */第55页/共93页定义形式类名 * 对象指针名;例Location A(5,10);Location *ptr;ptr=&A;通过指针访问对象成员第56页/共93页第57页/共93页#include class Location public:

29、 Location(int xx = 0 , int yy = 0) X = xx ; Y = yy ; void PrintObjectAddress() cout this = this Move(0,0); void Move(int x, int y) this-X = x; (*this).Y = y; int GetX() return X; int GetY() return Y;private: int X,Y;void main() Location A(5,8); Location *ptr; ptr = &A; cout A.X = GetX() endl; co

30、ut A.Y = (*ptr).GetY() endl; A.PrintObjectAddress(); cout &A = &A endl; cout A.X = GetX() endl; cout A.Y = (*ptr).GetY() endl;/* 程序运行结果 *A.X = 5A.Y = 8this = 0 x0012FF6C &A = 0 x0012FF6CA.X = 0A.Y = 0* 程序结束 */第58页/共93页void *malloc( size );参数size:欲分配的字节数返回值: 成功,则返回void型指针。 失败,则返回空指针。头文件:

31、和 第59页/共93页void free( void *memblock );参数memblock: 指针,指向需释放的 内存。返回值:无头文件: 和 第60页/共93页new 类型名T(初值列表)功能:在程序执行期间,申请用于存放T类型对象的内存空间,并依初值列表赋以初值。结果值:成功:T类型的指针,指向新分配的内存。失败:0(NULL) 第61页/共93页delete 指针P功能:释放指针P所指向的内存。P必须是new操作的返回值。 第62页/共93页第63页/共93页/ using the new operator#include void main() int * pi = new i

32、nt; *pi = 1001; cout int value = *pi : location = pi n; cout size of pi = sizeof pi; cout : size of *pi = sizeof *pi n; delete pi; double * pd = new double; *pd =; cout double value = *pd : location = pd n; cout size of pd = sizeof pd; cout : size of *pd = sizeof *pd n; delete pd;/* 程序运行结果 *int valu

33、e = 1001: location = 0 x00440040size of pi = 4: size of *pi = 4double value = 1e+007: location = 0 x00440030size of pd = 4: size of *pd = 8* 程序结束 */第64页/共93页/ using the new operator for arrays#include void main() double * p = new double 3; p0 =; / p如同一个数组名 p1 =; p2 =; cout p1 is p1 .n; p = p + 1; /

34、p毕竟是一个指针 cout Now p0 is p0 and ; cout p1 is p1 .n; p = p - 1; delete p;/* 程序运行结果 *p1 is 0.5.Now p0 is 0.5 and p1 is 0.8.* 程序结束 */第65页/共93页/ a function returning a pointer to char#include char * buildstr(char c, int n); void main() int times; char ch; cout ch; cout times; char *ps = buildstr(ch, time

35、s); cout ps n; delete ps; ps = buildstr(#, 5); cout ps -DONE- ps 0) pstrn = c; return pstr;/* 程序运行结果 *Enter a character: tEnter an integer: 3ttt#-DONE-#* 程序结束 */第66页/共93页动态分配数组时应注意:用new创建多维数组:new 类型名T下标表达式1下标表达式2;如果内存申请成功,new运算返回一个指向新分配内存首地址的指针,是一个T类型的数组,数组元素的个数为除最左边一维外各位下标表达式的乘积。第67页/共93页fpfp+1fp00

36、fp01fp02fp10fp11fp12例如:char (*fp)3;fp = new char23;第68页/共93页#include void get(double*&,int&);void print(double*,int);void main() double * a; int n; get(a,n); print(a,n); delete a; get(a,n); print(a,n); delete a; void get(double*& a, int& n) cout n; a = new doublen; cout 请输入 n 个实型数:n;

37、 for (int i = 0; i ai;void print(double* a, int n) for (int i = 0; i n; i+) cout ai ; cout endl;/* 程序运行结果 *请输入需要数据的个数: 3请输入3个实型数:2 5 82 5 8请输入需要数据的个数: 5请输入5个实型数:* 程序结束 */第69页/共93页#include class Locationpublic: Location() X = 0; Y = 0; cout 缺省构造函数. endl; Location(int xx , int yy)X = xx;Y = yy; cout 构

38、造函数. endl; Location( )cout 析构函数. endl; void Location:Move(int x, int y)X = x;Y = y;private: int X,Y;void main( ) coutStep One:endl; Location *Ptr = new Location; delete Ptr; coutStep Two:endl; Ptr = new Location(1,2); delete Ptr; Ptr=new Location2; Ptr0.Move(5,10); Ptr1.Move(15,20); coutDeleting.end

39、l; delete Ptr;/* 程序运行结果 *Step One:缺省构造函数.析构函数.Step Two:构造函数.析构函数.缺省构造函数.缺省构造函数.Deleting.析构函数.析构函数.* 程序结束 */第70页/共93页当函数的返回值是地址时,该函数就是指针形函数。定义形式 存储类型 数据类型 *函数名() 第71页/共93页/ a function returning a pointer to char#include char * buildstr(char c, int n); void main() int times; char ch; cout ch; cout tim

40、es; char *ps = buildstr(ch, times); cout ps n; delete ps; ps = buildstr(#, 5); cout ps -DONE- ps 0) pstrn = c; return pstr;/* 程序运行结果 *Enter a character: tEnter an integer: 3ttt#-DONE-#* 程序结束 */第72页/共93页定义形式 存储类型 数据类型 (*函数指针名)();含义: 数据指针指向数据存储区,而函数指针指向的是程序代码存储区。第73页/共93页/ 指向函数的指针#include double zhang

41、(int lns)return 0.02 * lns;double li(int lns) return 0.01 * lns + 0.0002 * lns * lns;void estimate(int lines, double (*pf)(int);void main() int code; cout code; cout 小张估计: ; estimate(code, zhang); cout 小李估计: ; estimate(code, li);void estimate(int lines, double (*pf)(int) cout lines 行代码需花费 (*pf)(line

42、s) 小时.n;/* 程序运行结果 *请输入大约需要编写多少行代码: 350小张估计: 350 行代码需花费 7 小时.小李估计: 350 行代码需花费 28 小时.* 程序结束 */第74页/共93页字符数组的定义和引用例:static char str8=112,114,111,103,114,97,109,0;static char str8=p,r,o,g,r,a,m,0;static char str8=program;static char str =program;字符串 字符串常量,例如:china 没有字符串变量,用字符数组来存放字符串 字符串以0为结束标志 注意字符数组的初

43、始化形式第75页/共93页第76页/共93页#includeint main() static char c10=I, ,a,m, ,a, ,b,o,y; int i; for( i = 0 ; i 10 ; i + ) cout ci; cout endl; return 0;运行结果: I am a boy第77页/共93页第78页/共93页#includeint main()static char diamond 5= , , * , , * , , * , * , , , , * , , * , , * , , , * ;int i,j;for ( i = 0 ; i 5 ; i + )for( j = 0 ; j 5 ; j + )cout diamondij;cout p1;/错误 p1=abc;/正确 p2=a; cin *p2; /正确第80页/共93页方法逐个字符输入输出将整个字符串一次输入或输出例:char c =China; cout str1 str2 str3; 运行时输入数据: How are you?内存中变量状态如下: str1: H o w 0 str2: a r e 0 str3: y o u ? 0内存中变量状态如下: str1: H o w 0 str2:

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论