复合类型
第二部分 指针
对指针和数组做的一些总结
1.声明指针
要声明指向特定类型的指针,请使用下面的格式
typeName *pointerName
double * pn; pn can point to a double value
double *pa;
char * pc; pn can point to a char value
其中,pn和pc都是指针,而double*和char*是指向double的指针和指向char的指针
2.给指针赋值
应将内存地址赋给指针。可以对变量名应用&运算符,来获得被命名的内存地址;
new运算符返回未命名的内存的地址
double bubble = 3.2;
pn = &bubble; assign address of bubble to pn
pc = new char; assign address of newly allocated char memory to pc
pa = new double[30]; assign address of 1st element of array of 30 double to pa
3.对指针解除引用
对指针解除引用意味着获得指针指向的值。对指针应用接触引用或间接值运算符(*)来解除引用。
因此,如果像上面例子那样,pn是指向bubble的指针,则*pn是指向的值,即3.2
cout << *pn; print the value of bubble
*pc = 's'; place 's' into the memory location whose address is pc
另一种对指针解除引用的方法是使用数组表示法,例如,pn[0]与*pn是一样的。
绝对不要对未被初始化为适当地址的指针解除引用
4.区分指针和指针所指向的值
如果pt是指向int的指针,则*pt不是指向int的指针,而是完全等同于一个int类型的变量。pt才是指针。
int *pt = new int; assigns an address to the pointer pt
*pt = 5; stores the value 5 at that address
5.数组名
在多数情况下,C++将数组视为数组的第一个元素的地址。
int tacos[10]; now tacos is the same as &tacos
一种例外情况是,将sizeof()运算符运用于数组名时,此时将返回整个数组的长度(单位为字节)
6.算术指针
C++允许将指针和整数相加。加1的结果等于原来的地址加上指向的对象占用的总字节数。
还可以将一个指针减去另一个指针,获得两个指针的差。后一种运算将得到一个整数,仅当两个指针指向
同一个数组(也可以指向超出结尾的一个位置)时,这种运算才有意义,这将得到两个元素的间隔
int tacos[10] = { 5,2,8,3,2,1,4,5,6,9 };
int *pt = tacos; suppose pt and tacos are the address 3000
pt = pt + 1; now pt is 3004 if a int is 4 bytes
int *pe = &tacos[9]; pe is 3036 if an int is 4bytes
pe = pe - 1; now pe is 3032,the address of tacos[8]
int diff = pe - pt; tacos[8] and tacos[1]
7.数组的动态联编和静态联编
使用数组声明来创建数组时,将采用静态联编,即数组的长度在编译时设置:
int tacos[10]; static binding,size fixed at compile time
使用new[]运算符创建数组时,将采用动态联编(动态数组),即将在运行时为数组分配空间,其长度也
在运行时设置。使用完这种数组后,应使用delete[]释放其占用的内存:
int size;
cin >> size;
int *pz = new int[size]; dynamic binding, size set at run time
...
delete[] pz; free memory when finished
8.数组表示法和指针表示法
使用方括号数组表示法等于对指针解除引用:
tacos[0] means *tacos means the value at address tacos
tacos[3] means *(tacos+3) means the value at address tacos + 3
数组名和指针变量都是如此,因此对于指针和数组名,既可以使用指针表示法又可以使用数组表示法
int *pt = new int[10]; pt points to block of 10 ints
*pt = 5; set element number0 to 5
pt[0] = 6; reset element number to6
pt[9] = 44; set tenth element (element number 9)to 44
int coats[10];
*(coats + 4) = 12; set coat[4] to 12