1. 指针定义和使用
示例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <iostream> using namespace std ;int main () { int a = 10 ; int * p; p = &a; cout << "a的地址为:" << &a << endl ; cout << "指针p为:" << p << endl ; *p = 1000 ; cout << "a = " << a << endl ; cout << "*p = " << *p << endl ; return 0 ; }
运行结果为:
1 2 3 4 a的地址为:0096FC40 指针p为:0096FC40 a = 1000 *p = 1000
示例2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std ;int main () { int * p = (int *)0x1100 ; cout << p << endl ; int a = 10 ; int * pc = &a; int * pp = (int *)&a; cout << "pc = " << hex << pc << endl ; cout << "pp = " << hex << pp << endl ; return 0 ; }
运行结果:
1 2 3 00001100 pc = 00DCFE18 pp = 00DCFE18
2. 指针所占内存空间 指针的值是一个代表内存地址的十六进制数,所占空间大小在32位操作系统下,占用四个字节;64位操作系统中占用8个字节。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> using namespace std ;int main () { int a = 10 ; int *p = &a; cout << "sizeof(p) = " << sizeof (p) << endl ; cout << "sizeof(int *) = " << sizeof (int *) << endl ; cout << "sizeof(float *) = " << sizeof (float *) << endl ; cout << "sizeof(double *) = " << sizeof (double *) << endl ; cout << "sizeof(char *) = " << sizeof (char *) << endl ; return 0 ; }
运行结果:
1 2 3 4 5 sizeof(p) = 4 sizeof(int *) = 4 sizeof(float *) = 4 sizeof(double *) = 4 sizeof(char *) = 4
3. 空指针 空指针:指针变量指向内存中编号为0的空间
用途:初始化指针变量
注意:空指针指向的内存不可以访问,0~255之间的内存编号是系统占用的,因此不可以访问。
示例:
1 2 3 4 5 6 7 8 #include <iostream> using namespace std ;int main () { int * p = NULL ; cout << p << endl ; return 0 ; }
运行结果:
4. 野指针 野指针:指针变量指向非法的内存空间
示例:
1 2 3 4 5 6 7 #include <iostream> using namespace std ;int main () { int * p = (int *)0x1100 ; return 0 ; }
5. const修饰指针
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std ;int main () { int a = 10 ; int b = 10 ; const int * p; p = &a; cout << "*p = " << *p << endl ; p = &b; cout << "*p = " << *p << endl ; a = 20 ; cout << "*p = " << *p << endl ; return 0 ; }
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std ;int main () { int a = 10 ; int b = 20 ; int * const p = &a; *p = 20 ; cout << "*p修改前" <<endl <<"a = " << a << endl ; cout << "*P修改后" << endl ; cout << "*p = " << *p << endl ; cout << "a = " << a << endl ; return 0 ; }
运行结果:
1 2 3 4 5 *p修改前 a = 20 *P修改后 *p = 20 a = 20
const既修饰指针,又修饰常量
特点:指针指向的值、指针的指向均不可以改变
1 2 3 4 5 6 7 8 9 10 #include <iostream> using namespace std ;int main () { int a = 10 ; int b = 20 ; const int * const p = &a; return 0 ; }
6.指针和数组
示例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <iostream> using namespace std ;int main () { int arr[10 ] = { 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 }; int * p = arr; cout << "数组首地址:" << arr << endl ; cout << "p = " << p << endl ; cout << "arr[0] = " << *p << endl ; p++; cout << "指针加一后" << endl ; cout << "p = " << p << endl ; cout << "arr[1] = " << *p << endl ; p = arr; for (int i = 0 ;i < 10 ;i++) { cout << *p << " " ; p++; } cout << endl ; return 0 ; }
运行结果:
1 2 3 4 5 6 7 数组首地址:0057F9C4 p = 0057F9C4 arr[0] = 1 指针加一后 p = 0057F9C8 arr[1] = 2 1 2 3 4 5 6 7 8 9 10
7. 指针和函数
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <iostream> using namespace std ;void Swap (int * a, int * b) ;int main () { int a = 10 ; int b = 20 ; cout << "交换前:" << endl ; cout << "a = " << a << endl ; cout << "b = " << b << endl ; Swap(&a, &b); cout << "交换后:" << endl ; cout << "a = " << a << endl ; cout << "b = " << b << endl ; return 0 ; } void Swap (int * a, int * b) { int temp; temp = *a; *a = *b; *b = temp; }
运行结果:
1 2 3 4 5 6 交换前: a = 10 b = 20 交换后: a = 20 b = 10
8.指针、函数、数组案例
封装一个函数,利用冒泡排序,实现对整型数组的升序排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <iostream> using namespace std ;void BubbleSort (int *arr,int len) ;int main () { int arr[10 ] = { 2 ,1 ,4 ,3 ,6 ,5 ,7 ,9 ,8 ,10 }; int len = sizeof (arr) / sizeof (arr[0 ]); BubbleSort(arr, len); for (int i = 0 ;i < len;i++) { cout << arr[i] << " " ; } cout << endl ; return 0 ; } void BubbleSort (int * arr, int len) { int * p = arr; int temp; for (int i = 0 ;i < len;i++) { p = arr; for (int j = 0 ;j < len - 1 - i;j++) { if (*p > *(p+1 )) { temp = *p; *p = *(p+1 ); *(p+1 ) = temp; } p++; } } }
运行结果: