#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;
int * p = NULL;
* p =100;
int arr [10] = {1,2,3,4,5,6,7,8,9,10};
int * p = arr;
cout<<"利用访问第一个元素:"<<*p<<endl;
p++;
cout<<"利用指针访问第二个元素:"<<*p<<endl;
int *p2 = arr;
for (int i=0;i<10;i++)
{
cout<<*p2++<<endl;
}
return 0;
}
a的地址:0x7ffe3d066ad4
指针p维:0x7ffe3d066ad4
a=1000
*p=1000
sandbox> exited with status 0
/run.sh: line 12: 13 Segmentation fault (core dumped) ./a.out
sandbox> exited with status 0
利用访问第一个元素:1
利用指针访问第二个元素:2
1
2
3
4
5
6
7
8
9
10
sandbox> exited with status 0
|