一.运算
1.指针在声明时若没有指向任何地址,最好赋初值NULL
2.(*p)++和*p++
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 int num[] = { 1, 4 }; 7 int *p = num; 8 9 (*p)++; 10 11 cout << *p << endl; // 2 12 13 *p++; 14 15 cout << *p << endl; // 4 16 17 return 0; 18 }
3.整型指针:iptr++ <==> iptr + 1*4
字符指针:iptr++ <==> iptr + 1*1
4.
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 int *p = new int(1024); 7 cout << *p << endl;//1024 初始值 8 }
二.一维数组
1.数组名是常量地址a[10]==>a++没有意义
2.
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 char str[] = "hello,world"; 7 cout << str + 5 << endl; //,world 8 cout << static_cast<void *>(str) << endl;//地址004FFDF0 9 cout << *(str + 5) << endl; //, 10 return 0; 11 }
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 int A[] = { 1, 3, 5, 7 }; 7 cout << A << endl; //001BFAB4 8 cout << A + 1 << endl; //001BFAB8 9 cout << &A << endl; //001BFAB4 指向数组
10 cout << &A + 1 << endl; //001BFAC4 11 cout << *(&A) << endl; //001BFAB4 12 cout << *(&A) + 1 << endl;//001BFAB8 13 return 0; 14 }
3.若一个数组名不出现在(sizeof,Alignof, &)后,则相当于指向数组首元素指针;
若出现,则数组名表示指向数组的指针。
4.&A相当于管辖范围上升了一级
*A相当于管辖范围下降了一级
5.形参数组名=指针变量
6.
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 char *p[3]; 7 cout << sizeof(p[0]) << endl; // 4 指针类数组,数组里每个元素是指针 8 9 char(*q)[3]; 10 cout << sizeof(q[0]) << endl; // 3 p是指针,指向的地方是char[3]数组 11 return 0; 12 }
7.
1 #include<iostream> 2 using namespace std; 3 4 int *getInt1() 5 { 6 int v1 = 20; 7 return &v1; 8 } 9 10 int *getInt2() 11 { 12 int v2 = 30; 13 return &v2; 14 } 15 16 int main() 17 { 18 int *p, *q; 19 p = getInt1(); 20 q = getInt2(); 21 22 cout << *p << endl; // 30 23 return 0; 24 }
三.二维数组
a相当于a[3][4]的第一个元素(包含4个int型元素的一维数组)的指针
*(*(p + i) + j)<==> p + i是第i + 1个地址,相当于&a[i]
*(p + i) + j<==> a[i]
*(*(p + i) + j)<==> a[i][j]
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 int a[3][4] = { {1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12} }; 7 for (int *p = &a[0][0]; p < &a[0][0] + 12; p++) 8 { 9 cout << *p << endl; // 1~12 10 } 11 cout << a << endl; //0018FE9C 12 cout << &a[0] << endl; //0018FE9C 13 cout << a + 1 << endl; //0018FEAC 14 cout << &a[0] + 1 << endl; //0018FEAC 15 cout << a[1] << endl; //0018FEAC 16 cout << &a[1] << endl; //0018FEAC 17 cout << *(a + 1) << endl; //0018FEAC 18 cout << *a + 1 << endl; //0018FEAC 19 cout << &a << endl; //0018FE9C 20 cout << &a + 1 << endl; //0018FECC 21 cout << &a[0][0] << endl; //0018FE9C 22 cout << &a[0][0] + 1 << endl; //0018FEA0 23 return 0; 24 }
a[0][0]=>&a[0][0]==a[0]=>&a[0]==a=>&a
*a == a[0], *a[0]==a[0][0]
四.new, delete
new和delete是内建操作符,关键字。malloc,calloc等是库函数
int *p = new int[100];
delete[] p;
五.类成员指针
1 #include<iostream> 2 using namespace std; 3 4 class Point 5 { 6 public: 7 int x, y; 8 Point(int x, int y) 9 { 10 this->x = x; 11 this->y = y; 12 } 13 }; 14 15 int main() 16 { 17 int Point::*p = &Point::x;//类成员指针<类型> <类名>::*<指针名> 18 Point p1(1, 2); Point p2(3, 4); 19 cout << p1.*p << endl; // 1 20 cout << p2.*p << endl; // 3 21 return 0; 22 }
六.指针与引用
1. int &m = a; &在此不是求地址
2. 声明引用时,必须同时初始化
3. 不能把引用名作为其他变量名的别名
4. 引用本身不占存储单元,故系统也不给引用分配存储单元。&m和&a求地址相等
5. 不能给数组建立引用
6. 指针通过地址间接访问变量,引用通过别名直接访问某个变量
void swap(int &p1, int &p2)//形参是引用 int a = 5, b = 10; swap(a, b); a = 10, b = 5
void print_row(ofstream &out, char c, int n)//输入输出流作为参数时,必须用引用调用
1 #include<iostream> 2 using namespace std; 3 4 //传值返回 5 int f1(int x) 6 { 7 return x; 8 } 9 //引用返回 10 int& f2(int x) 11 { 12 return x; 13 } 14 15 int main() 16 { 17 int y, x = 10; 18 y = f1(x);//x->临时变量->y 19 y = f2(x);//x->y 20 return 0; 21 }
使用引用返回时是一个实际单元,因此必须保证函数返回时该单元仍有效。
1 int& f2() 2 { 3 int i = 10; 4 return i; 5 }
当f返回时i已经不存在了,因此不能返回传回的i
七.函数指针
每个函数会占用一段连续的内存空间
函数名就是改函数所占内存空间的起始地址
可以将函数的入口地址赋给一个指针变量,使该指针变量指向该函数
1 #include <iostream> 2 using namespace std; 3 4 void min(int a, int b) 5 { 6 int num = a > b?b:a; 7 cout << num << endl; 8 } 9 10 void max(int a, int b) 11 { 12 int num = a > b?a:b; 13 cout << num << endl; 14 } 15 16 int main() 17 { 18 void (*p)(int, int); 19 p = min; 20 p(3,4);//3 21 p = max; 22 p(3,4);//4 23 return 0; 24 }