目录
1.定义指针
语法
1.数据类型 * 指针名称 =&变量;
2.指针名称 =&变量;
*p代表的就是指针地址所指向的变量
指针的地址:&p
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a = 100;
int b =300;
cout << a << endl;
int *p = &a; //初始化指针
cout <<"a="<< a << endl;
cout << "p=" << p << endl;
cout << "*p=" << *p << endl;
p = &a;
cout << "p=" << p << endl;
cout << "*p=" << *p << endl;
*p=200;
cout <<"a="<< a << endl;
cout <<"&*p="<< &*p << endl;//变量a的地址
cout <<"&p="<< &p << endl;//指针的地址
system("pause");
return 0;
}
2.特殊指针
空指针:int *p =NULL;
野指针:为被声明的内存地址,不可访问。
3.const 修饰指针
常量指针:const int *p =&a;
指针指向的对象可以更改
指针指向的值不可更改
指针常量 int * const p =&a;
指针指向的对象不可以更改
指针指向的值可更改
4.指针访问数组
#include <iostream>
#include <string>
using namespace std;
int main()
{
int arr0[] = {1, 2, 3, 4, 5};
double arr1[] = {1.1, 2.1, 3.1, 4.1, 5.1};
int *