c指针和动态内存分配PPT课件

上传人:牛*** 文档编号:75770859 上传时间:2022-04-16 格式:PPTX 页数:94 大小:348.37KB
返回 下载 相关 举报
c指针和动态内存分配PPT课件_第1页
第1页 / 共94页
c指针和动态内存分配PPT课件_第2页
第2页 / 共94页
c指针和动态内存分配PPT课件_第3页
第3页 / 共94页
点击查看更多>>
资源描述
Lecture 9: Pointers and Dynamic Memory (指针和动态内存分配)(指针和动态内存分配) Learn C+ through English and Chinese第1页/共94页2Chapter Nine: Pointers and Dynamic Memory (指针和动(指针和动态内存分配)态内存分配) Variable address (变量的地址)Pointer variables (指针变量)The dereference operator *(解引用运算符*)Using const with pointers (使用const修饰指针变量) Pointers and one-dimensional arrays(指针和一维数组) Pointers and multi-dimensional arrays (指针和多维数组) 第2页/共94页3 Pointers to structures (指向结构体的指针) Pointers to class objects (指向类对象的指针) Pointers as function arguments(指针变量作为函数实参) Dynamic memory allocation(动态内存分配) Chapter Nine: Pointers and Dynamic Memory (指针和动态内存(指针和动态内存分配)分配) 第3页/共94页49.1 Variable address(变量地址变量地址) Every variable object used in a C+ program is stored in a specific place in memory. Each location in memory has a unique address, in the same way that every house in a street has a unique address.(在C+程序中使用的每个变量和对象,都存储在内存中特定的存储单元中。每个存储单元都有唯一的地址,就像街道旁的每个房子都有唯一的地址一样。)第4页/共94页5Variable address(变量地址变量地址) 内存空间的访问方式 通过变量名访问 通过地址访问 地址运算符:&例:int var;则&var 表示变量var在内存中的起始地址第5页/共94页6 #include #include using namespace std ; void main() int var1 = 1 ; float var2 = 2 ; cout var1 has a value of var1 and is stored at &var1 endl ; cout var2 has a value of var2 and is stored at &var2 endl ; 第6页/共94页77.1.1 地址与指针地址与指针程序中程序中: int i; float k; 内存中每个字节有一个编号内存中每个字节有一个编号-地址地址.2000200120022005内存内存02003ik为其分配内存单元为其分配内存单元变量变量是对程序中数据是对程序中数据及存储空间的抽象及存储空间的抽象第7页/共94页89.2 Pointer variables(指针变量指针变量) A pointer variable is a variable that holds the address of another variable.(指针变量是存放另一变量地址的变量) data_type* variable_name; int* int_ptr; float* float_ptr; 其中,其中,“* *”表示后面声明的变量是指针类型的变表示后面声明的变量是指针类型的变量。指针变量一旦被赋值,我们就说该指针变量有了量。指针变量一旦被赋值,我们就说该指针变量有了指向。指向。“数据类型数据类型”可以是任意类型,指的是指针所可以是任意类型,指的是指针所指向的对象类型,这说明了指针所指向的内存单元可指向的对象类型,这说明了指针所指向的内存单元可以用于存放什么类型的数据,我们称之为指针的类型。以用于存放什么类型的数据,我们称之为指针的类型。区分:区分:int * p1, * p2; 与与 int *p1, p2;第8页/共94页9Pointer variables(指针指针 声明声明 例:例:int i; int *i_pointer=&i; 指向整型变量的指针指向整型变量的指针 使用使用 例例1: i=3; 例例2: *i_pointer=3; 语法形式语法形式 存储类型存储类型 数据类型数据类型 *指针名初始地址;指针名初始地址; 例:例: int *pa=&a;20003i_pointer*i_pointeri2000注意事项注意事项 用变量地址作为初值时,该变量必须在指针初始化之前已说用变量地址作为初值时,该变量必须在指针初始化之前已说明过,且变量类型应与指针类型一致。明过,且变量类型应与指针类型一致。 可以用一个已赋初值的指针去初始化另一可以用一个已赋初值的指针去初始化另一 个指针变量。个指针变量。 第9页/共94页10说明:说明: 在指针变量定义中,在指针变量定义中,*是一个是一个说明符说明符,它表明其后的变量,它表明其后的变量是指针变量,如在是指针变量,如在 int * p; 语句中,语句中,p是指针变量是指针变量,而不要认而不要认为为“*p”是指针变量;是指针变量; 指针变量定义时指定的指针变量定义时指定的数据类型数据类型不是指针变量本身的数据类不是指针变量本身的数据类型,而是指针变量所指向的对象(或称目标)的数据类型,型,而是指针变量所指向的对象(或称目标)的数据类型,指针变量指针变量只能指向只能指向定义时所规定类型的变量;定义时所规定类型的变量; 定义后值定义后值不确定,而指针变量一旦被赋值,就有了有效的指向对,而指针变量一旦被赋值,就有了有效的指向对象;象; 指针变量并不固定指向一个变量,可指向指针变量并不固定指向一个变量,可指向同类型同类型的的不同变不同变量量; 指针变量和普通变量的共同点是:它们都能存放数据,而指针变量和普通变量的共同点是:它们都能存放数据,而又有自己的地址。不同的是:普通变量中直接存放通常意又有自己的地址。不同的是:普通变量中直接存放通常意义下的数据,而指针变量中存放的是义下的数据,而指针变量中存放的是地址地址。第10页/共94页11.2000200420062005整型变量整型变量i10变量变量i_pointer2001200220032000指针指针指针变量指针变量 变量的变量的内容内容 变量的变量的地址地址指针变量指针变量变量变量变量地址变量地址( (指针指针) )变量值变量值指向指向地址存入地址存入指针变量指针变量指针变量和指针所指向的变量的区别:指针变量和指针所指向的变量的区别:对于:对于:int i=10, * i_pointer = &i; 变量的变量的地址地址 变量的变量的内容内容指针变量是用于存放内存单元地址指针变量是用于存放内存单元地址的变量,指针所指向的变量是指针变量的变量,指针所指向的变量是指针变量中所保存的内存地址对应的变量。中所保存的内存地址对应的变量。 第11页/共94页129.3 the dereference operator *(解引用运算符解引用运算符*) The dereference operator * is used to access the value of a variable, whose address is stored in a pointer.(解引用运算符*用于访问指针变量所指向的存储单元的内容。)int i=10, * i_pointer =&i;i_pointer = &i = &(*i_pointer)i = *i_pointer = *(&i) i_pointer-指针变量,它的内容是地址量指针变量,它的内容是地址量*i_pointer-指针的指针的目标变量目标变量,它的内容是数据,它的内容是数据&i_pointer- -指针变量占用内存的地址指针变量占用内存的地址第12页/共94页13#include using namespace std;main() int var =1; int* ptr; ptr=&var; / ptr contains the address of var cout ptr contains ptr endl ; cout “*ptr contains *ptr endl ; prt contains 0012FF88*ptr contains 1 第13页/共94页14#includeusing namespace std;int main()int *p1,*p2,*p,a,b;cinab;p1=&a;p2=&b;if(ab)p=p1;p1=p2;p2=p;couta=ab=bendl;coutmax=*p1min=*p2endl;return 0;第14页/共94页159.4 Using const with pointers(使用使用const修饰指针变量修饰指针变量) When defining a pointer, the pointer itself, the value it points to or both can be made constant. The position of const in the definition determines which of these apply.(定义指针变量时,指针变量本身、指针变量所指向的数据都可以声明为常量。变量定义中的const的位置决定了谁被声明为常量。)第15页/共94页16 int i=3,j=4;(a) const int* p=&i;/*p is a constant but p is not. *p=5; / Illegal: cannot change i using p. i=5; / Legal: i is not a constant. p=&j; / Legal: p now point to j. *p=4(b) int const* p=&i; / *p is a constant; p is not.(c) int* const p=&i; / p is a constant; *p is not. *p=5; /Legal: *p can be changed. i=5 p=&j; /Illegal: p is a constant.(d) const int* const p=&i; / both p and *p are constants. *p=5; /Illegal: *p is a constant. p=&j; /Illegal: p is a constant.第16页/共94页17指针与常量指针与常量 指向常变量的指针指向常变量的指针 不能通过指针来改变所指对象的值,但指针本身可以改变,可以指向另外的对象。例: const int n2=5;const int *pn= &n2; *pn=6; /错误例:const char *name1=“John”; char s =“abc”; name1=s; 对 *name1=1; 错第17页/共94页18定义指向常变量的指针变量形式:const 类型名 * 指针变量名如:const char *ptr;(1)如果一个变量已经被声明为常变量,只能用指向常变量的指针变量指向它,而不能用一般的指针变量去指向它。如:const char c=“boy”;const char*pp=c;char *p2=c; /不合语法。第18页/共94页19(2)指向常变量的指针变量除了可以指向常变量外,还可以指向未被声明为 const型的变量,此时不能通过指针变量改变该变量的值。char c1=a;const char *p;p=&c1;*p=b; /错误c1=b;第19页/共94页20指针与常量指针与常量 常量指常量指针针 若声明指针常量,则指针本身的值不能被改变。 但是它指向的整型变量是可以改变的。 例:int n1=3; const int n2=5;int *const pn= &n1; pn=&n2; /错误*pn=6; /正确例: char * const name1=“John”; name1=“abc”; 错第20页/共94页21例例 指向常量的指针做形参指向常量的指针做形参#includeconst int N=6;void print(const int *p,int n);void main() int arrayN; for(int i=0;iarrayi; print(array,N);第21页/共94页22void print(const int *p, int n) cout*p; for(int i=1;in;i+) cout.*(p+i); coutendl;1 2 3 4 5 61.2.3.4.5.6 第22页/共94页239.5 Pointers and one-dimensional arrays Ponters and arrays are directly related to one another . In c+ ,the name of an array is equivalent to the address of the first element of the array. The name of an array ,therefore ,is a pointer to the first element of the array . (指针和数组是相互联系的。在C+中,数组名代表数组的首地址。因此,数组名即为指向数组第一个元素的指针。) int a5; a &a0 a+1&a1 As the name of an array is a pointer to the first element of the array, the dereference operator * can be used to access the elements of the array.第23页/共94页24Pointers and one-dimensional arrays指向数组元素的指针int a10;int *p;p=&a0;/把a0元素的地址赋给指针变量p, 也就是使指针变量p指向a数组的第0号元素。它等价于p=a;/它表示把数组首元素的地址赋给p。int *p=&a0;它等价于int *p; p=&a0;也可以定义为 int*p=a;*p=1;第24页/共94页25Pointers and one-dimensional arrays如果指针变量p已经指向数组中得一个元素,则P+1指向同一数组中得下一个元素。int array 10=1,2,3,4,5,6,7,8,9,10,*p;当p=array时下面的等价关系成立:p=array=&array0/表示数组元素的首地址p+i=array+i=&arrayi;/表示第i个数组元素的地址*(p+i)=*(array+i)=arrayi/表示第i个数组元素指向数组的的指针变量也可以带下标,所以p+i和pi是一样的、第25页/共94页26引用数组元素引用数组元素可以有三种方法:(1)下标法: 数组名下标 a1(2)指针变量法:*指针变量 *(P+i)(3)首地址法:*(首地址+位移量 ) *(array+i)第26页/共94页27如果先使指针变量p指向数组a的首地址(p=a)则:(1)p+ 使p指向下一个元素,即a1,用*p可以得到下一个元素a1的值,(2)*p+ 先得到p指向的变量的值,然后再使P得值加1,使p指向下一个元素,for(p=a;pa+10;)cout*p+; (3)*(p+)和*(+P)不同,后者是先使P加1,在求*P的值。(4)(*p)+表示P所指向的元素值加1. 第27页/共94页28#include using namespace std ;main() int a5 = 10, 13 , 15 , 11, 6 ; for ( int i = 0; i 5; i+ ) cout Element i is *( a + i ) endl ; Element 0 is 10 Element 1 is 13 Element 2 is 15 Element 3 is 11 Element 4 is 6 第28页/共94页29 Although the name of an array is a pointer to the first element of the array, you cannot change its value; this is because it is a constant pointer.(虽然数组名是指向数组第一个元素的指针,但是由于它是一个常量指针,因此它的值是不能改变的。) int a10; float numbers100; a+; number+=2;Pointers and one-dimensional arrays第29页/共94页30 You can assign the name of an array to a pointer variable of the same type. int a5; int * p; p=a; / valid :assiqnment of a constant to a variable a+ ; / Invalia: the value of a costant cannot change. p+; / Valid:p is a variable. p now points to element 1 of the array a . p-; / Valid :point to element 1 of the array a . p +=10 ;/ Valid. But p is outside the range of the array a . / so *p is underfined . A common error. p=a -1; / Valid , but p is outside the ranger of the array .Pointers and one-dimensional arrays第30页/共94页31 A constant may be added to or subtancted from the value of a pointer. allowing access to different memory to locations. Hower ,not all arithmeic operations are permissible on poniters .For example ,the multiplication of two pointers is illegal,because the result would not be a valid memory address.(通过将指针变量加上或减去一个常量,可以使它指向不同的存储单元。但是,并非所有的算术运算符都可以应用于指针。例如,两个指针相乘是非法的,因为相乘的结果可能不是一个有效的内存地址。)Pointers and one-dimensional arrays第31页/共94页32分别用三种方式访问数组中的所有元素分别用三种方式访问数组中的所有元素void main( ) int i, array10= 1,2,3,4,5,6,7,8,9,10 , *p; coutNO1: ; for( i=0; i10; i+) coutarrayi= arrayi ; coutnNO2:n; for( i=0; i10; i+) cout*(array+“i)=*(array+i) ; coutnNO3(1)n; for( p=array, i=0; i10;i+) cout*(p+“i)=*(p+i) ; coutnNOs(2)n; for( p=array; parray+10; p+) cout*p+=“*p ; coutn; 第32页/共94页33 A two-dimensional array is stored as an array of arrays. This means that a is a one-dimensional array whose elements are themselves a one-dimensional arrays of integers . As with one-dimensonal array ,the name of a two dimensional array is pointer to the first element of the array .Therefore a is equivalent to &a 0 , a 0 is itself an array of two intergers,which means that a 0 is equivalent to &a00 . Pointers and multi-dimensional arrays第33页/共94页34a+2a0a00a03a10a13a20a23a1a2aa+1a0+0a0+3a1+3a2+3int a34; a13 *(a1+3) *(*(a+1)+3)第34页/共94页35#includeusing namespace std;int main()int a34=1,3,5,7,9,11,13,15,17,19,21,23;int *p;for(p=a0;pa0+12;p+)cout*p ;cout成员名成员名例:例: struct stu date, *pstu = &date; 则:则:pstu -num 等价于等价于 (*pstu).num第37页/共94页38指向结构体变量的指针指向结构体变量的指针#includeusing namespace std;int main() struct student int num; string name; char sex; float score; ;student stu;student*p=&stu;stu.num=10301;stu.name=WangFun;stu.sex=f;stu.score=89.5;coutstu.num stu.name stu.sex stu.scoreendl;cout(*p).num (*p).name (*p).score (*p).sex成员名 -指向运算符P-n得到P指向的结构体变量中的成员n的值。p-n+得到p指向的结构体变量中成员n的值,用完改值后使它加1.+p-n得到p指向的结构体变量中的成员n的值,并使之加1,然后再使用它。第39页/共94页40用指向结构体变量的指针作实参#include#includeusing namespace std;struct studentint num;string name;float score3;stu=12345,lifung,67.5,89,78.5;int main()void print(student *);student *pt=&stu;print(pt);return 0;void print(student*p)coutnum “name “score0“score1“score2成员名第42页/共94页43Class Time public: int hour; int minute;int sex; void get_time();Void Time:get_time() couthour“:”minute“:”sexhour p1所指向的对象中的hour成员,(*p1).get_time 调用p1所指向的对象中的get_time 函数P1-get_time 调用p1所指向的对象中的get_time 函数第44页/共94页45例例 对象指针应用举例对象指针应用举例void main( ) Point A(5,10); Point *ptr; ptr=&A; int x; x=ptr-GetX( ); coutxendl;第45页/共94页46Example#include #include #include bank_ac.h#include bank_ac.cppusing namespace std ;main() bank_account ac ; / ac is a bank_account object. bank_account* ac_ptr ; / ac_ptr is a pointer to a bank_account. ac_ptr = &ac ; / ac_ptr contains the address of the object ac. ac_ptr - deposit( 100 ) ; ac_ptr - display_balance() ; ac-ptr - deposit (100) ; ( *ac_ptr) .deopsit(100); 第46页/共94页47 9.9 Pointers as funtion arguments Like any data type, pointers can be used as function arguments.第47页/共94页48#include using namespace std ;void swap_vals( float* val1, float* val2 ) ; main() float num1 , num2 ; cout num1 ; cin num2 ; if ( num1 num2 ) swap_vals( &num1, &num2 ) ; cout The numbers in order are num1 and num2 endl ; void swap_vals( float* ptr1, float* ptr2 ) float temp = *ptr1 ; *ptr1=*ptr2;*ptr2=temp;第48页/共94页499.10 Dynamic memory allocation C+ has the ability to allocate memory while a program is executing. This is done by using the memory allocation operator new.第49页/共94页50 Dynamic memory allocation动态申请内存操作符 newnew 类型名T(初值列表)功能:在程序执行期间,申请用于存放T类型对象的内存空间,并依初值列表赋以初值。结果值:成功:T类型的指针,指向新分配的内存。失败:0(NULL)第50页/共94页51new运算符的例子;new int;/开辟一个存放整数的存储空间,返回一个指向该存储空间的地址(即指针)new int(100);/开辟一个存放整数的存储空间,并指定该整数的初值为100,返回一个指向该存储空间的地址(即指针),new char10;/开辟一个存放字符数组的(包含10个元素)空间,返回首元素的地址。new int54;/开辟一个存放二维整型数组(大小为5*4)的空间,返回首元素的地址。float *p =new float();/开辟一个存放单精度数的空间,并指定该实数的初值是,将返回的该空间的地址赋给指针变量p。第51页/共94页52释放内存操作符释放内存操作符deletedelete 运算符使用的一般格式为:delete 指针P功能:释放指针P所指向的内存。P必须是new操作的返回值。delete p;new char10;如果把 new返回的指针付给指针变量pt,则用 delete pt;/在指针变量前面加一对方括号,表示是对数组空间的操作。第52页/共94页53#include#includeusing namespace std;struct studentchar * name;int num;char sex;int main() student*p;p=new student;p-name=wang Fun;p-num=10123;p-sex=m;coutnameendlnumendlsexendl;delete p;return 0;第53页/共94页54例例 动态存储分配举例动态存储分配举例#include struct date int month; int day; int year;第54页/共94页55void main( ) int index, *point1, *point2; point1 = &index; *point1 = 77; point2 = new int; *point2 = 173; cout The values are index *point1 *point2 n; delete point2; point1 = new int; point2 = point1; *point1 = 999; cout The values are index *point1 *point2 month = 10; date_point-day = 18; date_point-year = 1938; cout month / day / year n; delete date_point; /释放结构体第57页/共94页58 char *c_point; c_point = new char37; /动态分配数组 delete c_point; /释放数组 c_point = new charsizeof(date) + 133; /动态分配数组 delete c_point; /释放数组 return 0;运行结果:The values are 77 77 173The values are 77 999 99910/18/1938第58页/共94页59对象的动态建立和释放对象的动态建立和释放如果已经定义了一个box类,可以用下面的方法动态的建立一个对象:new box;定义一个指向本类的对象的指针变量来存放地址box *pt;pt=new box;第59页/共94页60在程序中就可以通过pt访问这个新建的对象,coutheighr;coutvolume();c+还允许在执行new时,对新建的对象进行初始化。可以使用delete运算符对新建的对象所占有的空间进行套释放。delete pt;在执行运算符时,自动调用析构函数。第60页/共94页61例例 动态创建对象举例动态创建对象举例#includeclass Point public: Point( ) X=Y=0; coutDefault Constructor called.n; Point(int xx,int yy) X=xx; Y=yy; cout Constructor called.n; Point( ) coutDestructor called.n; int GetX( ) return X; int GetY( ) return Y;void Move(int x,int y) X=x; Y=y; private: int X,Y;第61页/共94页62void main( ) coutStep One:endl; Point *Ptr1=new Point; delete Ptr1; coutStep Two:endl; Ptr1=new Point(1,2); delete Ptr1;运行结果:运行结果:Step One:Default Constructor called.Destructor called.Step Two:Constructor called.Destructor called.第62页/共94页63例例 动态创建对象数组举例动态创建对象数组举例#includeclass Point /类的声明同例6-15,略;void main( ) Point *Ptr=new Point2; /创建对象数组 Ptr0.Move(5,10); /通过指针访问数组元素的成员 Ptr1.Move(15,20); /通过指针访问数组元素的成员 coutDeleting.endl; delete Ptr; /删除整个对象数组运行结果:运行结果:Default Constructor called.Default Constructor called.Deleting.Destructor called.Destructor called.第63页/共94页64访问对象的数据成员访问对象的数据成员#include using namespace std;class Humanpublic:int get()constreturn i;void set(int x)i=x;private:int i;int main()Human *p=new Human;p-set(1);coutget()endl;delete p;return 0;运行结果:运行结果:1第64页/共94页65在构造函数中开辟空间在构造函数中开辟空间#include using namespace std;class Humanpublic:int get()constreturn *i;void set(int x)*i=x;Human();Human();private:int *i;Human:Human()cout构造函数执行中.n;i=new int(999);int main()Human *p=new Human;coutget()set(1);coutget()endl;delete p;return 0;Human:Human()cout析构函数执行中.n;delete i;构造函数执行中9991析构函数执行中第65页/共94页669.10.1 Allocating memory dynamically for an array The new memory allocation operator can be used to allocate a contiguous block of memory for an array of any type, whether the data type is built-in or is a user-defined structure of class. pointer = new data_typesize ; For example: / Allocate memory for 10 integers. int_ptr = new int10; / Allocate memory for 5 bank_account objects. ac_ptr = new bank_account5;When allocating memory for an array of class objects, there must be a default constructor for the class so that the elements of the array get initialised.(当为一个类对象数组动态分配内存时,这个类必须有自己的默认构造函数来初始化类对象数组元素。)第66页/共94页67/dynamic memory allocation for an array of integers.#includeusing namespace std;main() int* int_array; int no_els, i; cout no_els; int_array = new intno_els; for ( i = 0 ; i no_els ; i+) cout “Enter element ”i int_arrayi; for( i = 0;i no_els;i+) cout“Element”i“ is “*(int_array+i)endl; delete int_array;Enter the number of elements 2Enter element 0: 57Enter element 1: 69Element 0 is 57Element 1 is 69第67页/共94页68 The elements of the newly allocated array can be accessed using either a pointer or an index.(可以使用指针或下标方式访问新分配内存的数组元素。) int_array1*(int_array+1) The allocated memory is freed using the delete operator. It is important to remember to include the square brackets when freeing memory for a previously allocated array. Without the square brackets, only the first element of the array will be deleted.(可以使用delete运算符释放已分配的内存。当为一个数组释放先前动态分配的内存时,必须用方括号。如果不加释放的仅是数组的第一个元素的内存。)第68页/共94页699.10.2 Initialisation with newuWhen allocating memory for an array of class objects, the default constructor for the class initialises the elements of the array.(当为一个类对象的数组动态分配内存时,会自动调用类的默认构造函数对数组元素进行初始化。) Ac_ptr = new bank_account5;results in the default constructor for the bank account class being called five times.uNo initialisation is done for dynamically allocated arrays of built-in data types. (对内置数据类型的数组动态分配内存时无需对其初始化。)For example, int_ptr = new int10;results in the ten non-initialised integer elements.第69页/共94页70uSingle instances of any data type(built-in, a user-defined structure or a class) can be initialised using a second form of the new operator.(对任意数据类型的单个实例,可以使用new运算符的另一种形式对其进行初始化。) pointer = new data_type( initial_value);Eg. /allocate memory for an integer, with an initial value of 100. int_ptr = new int(100); /allocate memory for an integer, with no initial value. int_ptr = new int; /allocate memory for a bank_account object. The default constructor is /called to do the initialisation. ac_ptr = new bank_account; /allocate memory for a bank_account object. A constructor is called to /assign initial values to account number and balance. ac_ptr = new bank_account(1234,100);初始值是可选的第70页/共94页71#include#include#include “bank_ac.h”#include “bank_ac.cpp”using namespace std;main() int no_of_acs; cout no_of_acs; bank_account* accounts = new bank_accountno_of_acs; cout endl “Accounts:” endl; for(int i = 0;i no_of_acs;i+) accountsi.display_balance(); cout endl;第71页/共94页72cout“bank_ptr1:”display_balance();coutendl;bank_account* bank_ptr2=new bank_account(123,100);cout“bank_ptr2:”display_balance();delete accounts;delete bank_ptr1;delete bank_ptr2; 运行结果运行结果Enter the number of bank accounts 2Account:bank_ptr1:bank_prt2:第72页/共94页739.10.3 allocating memory for multi-dimensional arrays In C+, multi-dimensional arrays are implemented as arrays of arrays. To fully understand dynamic memory allocation for multi-dimensional arrays, familiarity with section 9.6 is necessary.第73页/共94页74#include#includeusing namespace std;main() int no_of_rows, no_of_cols; int i,j; float *data; coutno_of_rows; cout no_of_cols;/allocate storage for the rows. data=new float* no_of_rows;/allocate storage for each colum for( j=0;jno_of_rows;j+) data j=new floatno_of_cols;for(i=0;ino_of_rows;i+) for(j=0;jno_of_cols;j+) dataij=i*10+j;for(i=0;ino_of_rows;i+) for(j=0;jno_of_cols;j+) coutdataij ;coutendl;/delete the columns.for(i=0;ino_of_rows;i+) delete datai;/delete the rows.delete data;第74页/共94页75字符串与指针字符串与指针定义一个字符指针并令其指向一个字符串char *strp=“Hello!”;等价于:char * strp;strp=“Hello!”;第75页/共94页76指向字符串的指针与字符数组的区别指向字符串的指针与字符数组的区别:char *ch=“hello world”;char ch=“hello world”;1.赋值不同:对于字符数组,我们可以对某个元素进行赋值char ch20;ch=“hello world”;错误。但是char *ch; ch=“hello world”;正确。赋给指针变量ch的是字符串的首地址,而不是字符串。第76页/共94页772.值不同指针变量的值可以改变,但是数组名的值不可改变。字符串指针变量保存的是字符串的首地址,因此值可以改变。char *ch=“hello world”;ch=ch+6;coutch;cina;第77页/共94页78注意!若有如下声明:char a4, *p1, *p2; 错误的:a=abc;cinp1; 正确的:p1=abc;p2=a; cinp2;第78页/共94页79字符串与指针字符串与指针(1)用字符串指针指向一个字符串#includeusing namespace std;int main()char *str=I LOVE CHINA!;/char str=“I love CHINA!” /string str=“I love CHINA!”coutstrendl;return 0;第79页/共94页80 以地址方式传递数据,可以用来返回函数处理结果。 实参是数组名时形参可以是指针。第80页/共94页81 题目:读入三个浮点数,将整数部分和小数部分分别输出#include void splitfloat(float x, int *intpart, float *fracpart) /形参intpart、 fracpart是指针 *intpart = int(x);/ 取x的整数部分 *fracpart = x - *intpart; /取x的小数部分 第81页/共94页82void main(void)int i, n;float x, f;cout Enter three (3) floating point numbers endl;for (i = 0; i x;splitfloat(x,&n,&f); /变量地址做实参cout Integer Part is n Fraction Part is f endl;第82页/共94页83运行结果:Enter three (3) floating point numbers 第83页/共94页84例例 输出数组元素的内容和地址输出数组元素的内容和地址#include #include void Array_Ptr(long *P, int n) int i;cout In func, address of array is unsigned long(P) endl;cout Accessing array in the function using pointers endl;for (i = 0; i n; i+)cout Address for index i is unsigned long(P+i);cout Value is *(P+i) endl;第84页/共94页85void main(void)long list5 = 50, 60, 70, 80, 90;cout In main, address of array is unsigned long(list) endl;cout endl; Arr
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!