数组指针与字符串

上传人:宝路 文档编号:47917723 上传时间:2018-07-06 格式:PPT 页数:93 大小:521.82KB
返回 下载 相关 举报
数组指针与字符串_第1页
第1页 / 共93页
数组指针与字符串_第2页
第2页 / 共93页
数组指针与字符串_第3页
第3页 / 共93页
数组指针与字符串_第4页
第4页 / 共93页
数组指针与字符串_第5页
第5页 / 共93页
点击查看更多>>
资源描述

《数组指针与字符串》由会员分享,可在线阅读,更多相关《数组指针与字符串(93页珍藏版)》请在金锄头文库上搜索。

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

2、.55;cout void main() const int SIZE=5;double aSIZE;int i;cout ai;cout = 0; i -)cout void main() float a = 22.2, 44.4, 66.6 ;int size = sizeof(a)/sizeof(float);for (int i=0; ivoid main() double a = 22.2, 44.4, 66.6 ;double x=11.1;cout void main() int i ;static int f20 = 1 , 1 ; /初始化第0、1个数for(i = 2; i

3、 void read(int a , int 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 an; while (an+ != 0);-n; / dont count the 0 void print(int a , int n)for (int i=0; i void RowSum(int A 4, int nrow) for (int i = 0; i #include “Location.

4、h“ Location:Location() X = Y = 0;cout #include “Location.h“int main() cout void main() int *p ;int i ; / int *p , i ;p = i=10;cout void main() const int SIZE = 3;int aSIZE = 55, 66, 77 ;cout void main() int a = 22, 33, 44, 55, 66;int *p;cout ai ;cout ai ;cout ai ;cout void swap(int *p1, int *p2) int

5、 p; p=*p1; *p1=*p2; *p2=p; void main( ) int a,b; int *p1, *p2; cin a b; p1 = p2 = if ( a void invert(int *p ,int n) /实参是数组名时形参可以是指针。int temp, i , j , m = (n-1)/2;for(i=0;i void main( ) int line1 =1,0,0; /声明数组,矩阵的第一行 int line2 =0,1,0; /声明数组,矩阵的第二行 int line3 =0,0,1; /声明数组,矩阵的第三行 int *p_line3;/声明整型指针数组

6、 p_line0=line1;/初始化指针数组元素 p_line1=line2; p_line2=line3; cout int main() int *pt_int;float *pt_float;int pig = 7, dog = 27;float x = 1.2345, y = 32.14;void *general; pt_int = *pt_int += dog;cout const int N=6; void print(const int *p,int n); void main() int arrayN;for(int i=0;iarrayi;print(array,N);

7、void print(const int *p, int n) cout-成员名成员名 (* (*对象指针名对象指针名). ).成员名成员名 对象指针应用举例对象指针应用举例#include class Location public:Location(int xx = 0 , int yy = 0) X = xx ; Y = yy ; void PrintObjectAddress()cout Move(0,0);void Move(int x, int y)this-X = x;(*this).Y = y; int GetX() return X;int GetY() return Y;

8、private:int X,Y; ;void main() Location A(5,8);Location *ptr;ptr = coutGetX() GetX() 和 动态内存释放函数动态内存释放函数void free( void *memblock );参数memblock:指针,指向需释放的 内存。返回值:无头文件: 和 动态申请内存操作符动态申请内存操作符 newnewnew 类型名T(初值列表)功能:在程序执行期间,申请用于存放T类型对象的内存空间,并依初值列表赋以初值。结果值:成功:T类型的指针,指向新分配的内存。失败:0(NULL) 释放内存操作符释放内存操作符deletede

9、letedelete 指针P功能:释放指针P所指向的内存。P必须是new操作的返回值。 动态存储分配举例动态存储分配举例/ using the new operator #include void main() int * pi = new int;*pi = 1001;cout void main() double * p = new double 3; p0 = 0.2; / p如同一个数组名p1 = 0.5;p2 = 0.8;cout char * buildstr(char c, int n); void main() int times;char ch;cout ch;cout ti

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

11、= new char23;#include void get(double* 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 = new doublen;cout ai; void print(double* a, int n) for (int i = 0; i class Location public:Location() X = 0; Y = 0; cout char

12、 * buildstr(char c, int n); void main() int times;char ch;cout ch;cout times;char *ps = buildstr(ch, times);cout 0)pstrn = c;return pstr; /* 程序运行结果 * Enter a character: t Enter an integer: 3 ttt #-DONE-# * 程序结束 */指向函数的指针指向函数的指针定义形式存储类型 数据类型 (*函数指针名)();含义:数据指针指向数据存储区,而函数指针指向的是程序代码存储区。/ 指向函数的指针 #inclu

13、de double zhang(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 int main() static char c10=I, ,a,m, ,a, ,b,o,y;int i;for( i = 0 ; i int main() static char diamond 5= , , * , , * , ,

14、 * , * , , , , * , , * , , * , , , * ; int i,j; for ( i = 0 ; i p1;/错误p1=“abc“;/正确p2=a; cin *p2; /正确字符串的输入字符串的输入/ /输出输出 方法 逐个字符输入输出 将整个字符串一次输入或输出 例:char c =“China“;cout str1 str2 str3;运行时输入数据:How are you?内存中变量状态如下: str1: H o w 0 str2: a r e 0str3: y o u ? 0内存中变量状态如下: str1: H o w 0 str2: a r e 0str3: y o u ? 0若改为:static char str13;cin str;运行时输入数据:How are you?内存中变量 str 内容如下: str: H o w 0 整行输入字

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

最新文档


当前位置:首页 > 中学教育 > 教学课件

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