文章目录 1.声明指针 2.初始化指针 3.指针地址和大小,值 4.指针解引用,修改值 5.指针指向堆内存,修改值 6.申请堆内存并释放 7.数组释放 8.指针运算 9.指针递增 10.指针递减 11.指针常量 12.常量指针 13.常量指针指向常量
1.声明指针
2.初始化指针
3.指针地址和大小,值
4.指针解引用,修改值
5.指针指向堆内存,修改值
6.申请堆内存并释放
7.数组释放
8.指针运算
9.指针递增
10.指针递减
11.指针常量
12.常量指针
13.常量指针指向常量
#include <iostream>
#include <string>using namespace std;/*
* 1.声明指针
* 2.初始化指针
* 3.指针地址和大小,值
* 4.指针解引用,修改值
* 5.指针指向堆内存,修改值
* 6.申请堆内存并释放
* 7.数组释放
* 8.指针运算
* 9.指针递增
* 10.指针递减
* 11.指针常量*12.常量指针* 13.常量指针指向常量
* */int main(){int * i;string * s;int age = 10;int * p = &age;int * dogName = nullptr;cout << "指针的地址:"<< &p << endl;cout << "指针的值:"<< p << endl;cout << "age得地址:"<< &age << endl;cout << "指针的大小:"<< sizeof(p) << endl;cout << "age的值:"<< age << endl;string name = "小明";string *n = &name;cout << "指针解引用:"<< *n << endl;*n = "小红";cout << name << *n << endl;int * score = new int(10);*score = 100;cout << "score="<<*score << endl;int * p2 = new int;p2 = nullptr;delete p2;int *arr = {nullptr};arr = new int[3];delete[] arr;int id[] = {1,2,3};int *pd = id;cout << "pd得值" << pd << endl;cout << "id得地址:"<< id << endl;int score2[] = {10,20,30};int *pscore = score2;cout << *pscore << "==" << score2[0] << endl;cout << *(pscore+1) << "==" << score2[1] << endl;for (int j = 0; j < 3; ++j) {cout << *pscore <<endl;pscore++;}int score3 = 59;const int * p3 = &score3;// 只能修改指针的指向,不能修改值
// p3 = 100;int age3 = 18;p3 = &age3;cout << *p3 << endl;int * const p4 = &score3;
// p4 = &age3;*p4 = 69;cout << *p4 << endl;const int * const p5 = &score3;
// p5 = &age3;
// *p5 = 102;cout << p5 << endl;return 0;
}