- 1 指针定义和使用
- 2 指针的内存
- 3 空指针和野指针
- 3.1 空指针
- 3.2 野指针
- 4 const 修饰指针和常量
- 5 指针和数组
- 6 指针和函数
- 7 指针、数组、函数的综合案例
#includeusing namespace std; int main() { //1 定义指针: 数据类型 * 指针变量名; int a = 10; int * p; p = &a; cout<<"a的地址为:"<<&a< 2 指针的内存 #includeusing namespace std; int main() { //1 定义指针: 数据类型 * 指针变量名; int a = 10; int * p = &a; cout<<"sizzeof(int *):"< 64位系统输出为8
3 空指针和野指针 3.1 空指针
#include3.2 野指针using namespace std; int main() { // 1 用途:初始化指针变量 // 2 注意:空指针的内存是不可访问的:0-255是系统占用的 int * p = NULL; *p = 100; return 0; } #includeusing namespace std; int main() { int * p = (int *)0x1100; cout<<*p< 4 const 修饰指针和常量 #includeusing namespace std; int main() { int a = 10; int b = 10; const int *p =&a; cout<<*p< 5 指针和数组 #includeusing namespace std; int main() { // 利用指针来访问数组中的元素 int arr[10] = {1,2,3,4,5,6,7,8,9,10}; cout<<"第一个元素"< 6 指针和函数 #includeusing namespace std; void swap01(int *p1,int *p2){ int temp = *p1; *p1 = *p2; *p2 = temp; } int main() { int a = 10; int b = 20; swap01(&a,&b); cout<<"a="< 7 指针、数组、函数的综合案例 #includeusing namespace std; void BubbleSort(int *arr,int len) { for(int i=0;i arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } void PrintArr(int *arr,int len) { for (int i = 0; i



