本部分罗列了本章的难点或注意点,以便复习巩固或者回忆,关注私聊我可以免费领取pdf版C++ Primer Plus 第6版。
-
函数不能直接返回数组,但可以将数组作为结构或对象组成部分返回。
-
交替乘除运算的策略可以防止中间结果超出最大的浮点数 result = result *a/b;
-
数组名是指针,指向第一个元素。也有些例外,数组声明使用数组名来标记存储位置;对数组名使用sizeof将得到数组的长度;将地址运算符&用于数组名时将返回整个数组的地址。
-
传递数组任符合按值传递,将数组的地址传给一个新变量。
-
形参的数组实际是指针类型,sizeof 数组形参==sizeof pointer;
-
两种传递数组的方式:1.把数组起始指针和数组长度当做参数传递;2.利用两个指针传递。
-
禁止将const的地址赋给常规指针(可以通过强制类型转换突破限制)。能将const的地址赋给const指针,也能将常规地址赋给const指针。
-
仅当一层间接关系时,才可以将非const地址或指针赋给const指针。下面是二层间接关系的错误演示:
const int **pp2; int * p1; const int n = 13; pp2 = &p1; // invalid *pp2 = &n; *p1 = 10;
-
const int * pointer 能修改pointer的指向,但不能通过指针修改指向的数据 int* const pointer 不能修改pointer的指向,但能通过指针修改指向的数据
const int* const pointer 不能修改pointer的指向,也不能通过指针修改指向的数据
-
++*p *++p 从右向左 *p++ 从左向右
-
int data[3][4] //data的类型是指向由4个int组成的数组的指针
-
int (*arr)[4]arr指向由4个int组成的数组的指针等效于int arr[] [4] ; int *arr[4] arr中每一个元素都是指针
-
二维数组传递参数:传递一个数组的指针和行数。
-
arr[r][c] == *(*(ar2+r)+c); ar2; //pointer to first row of an array of 4 int ar2+r; //pointer to row r(an array 0f 4 int) *(ar2+r); //row r(an array of 4 int,hence the name of an array,thus a pointer to the first int in the row,i.e.,ar2[r]) *(ar2+r)+c; //pointer int number c in row r i.e. ,ar2[r]+c *(*(ar2+r)+c); //value of int number c in row r, i.e. ar2[r][c]
-
处理字符串中字符的标准方式:
while(*str){ statements; str++; }
-
const int Seasons = 4; const array < string, Seasons >Snames = {"1","2","3","4"}; const array *pa = &Snames; cout << (*pa)[0];
-
函数名即是函数的地址
-
用函数指针调用函数
double a(int); double (*a)(int); (*a)(int); //a(int) 也可以
-
auto只能用于单值初始化,而不能用于初始化列表
-
typedef也可用来简化代码量
typedef const double *(*p_fun)(const double *,int); //p_fun now a type name p_fun p1 = f1; p_fun pa[3] = {f1,f2,f3}; p_fun (*pd)[3] = &pa;