&i i的地址,*pp指向的内容
指针的指针

int main(){int i = 30;cout << "&i = " << &i << endl;cout << "i = " << i << endl;int *pi = &i;cout << "*pi = " << *pi << endl;cout << "&pi = " << &pi << endl;int **ppi = πcout << "**ppi = " << **ppi << endl;system("pause");return 0;}&i = 0x61ff08i = 30*pi = 30&pi = 0x61ff08**ppi = 30
间接数据访问
改变一级指针指向
int main()
{
int i = 30;
int *pi = &i;
cout << "一级指针*pi= " << *pi << endl; // 一级指针*pi= 30
cout << "一级指针pi= " << pi << endl; // 一级指针pi= 0x61ff08
int **ppi = π
cout << "二级指针**pi= " << **ppi << endl; // 二级指针**pi= 30
cout << "二级指针ppi= " << ppi << endl; // 二级指针ppi= 0x61ff04
*pi = 20;
cout << "改变一级指针值 *pi = " << *pi << endl; // 改变一级指针值 *pi = 20
cout << " pi = " << pi << endl; // pi = 0x61ff08
cout << "二级指针**ppi= " << **ppi << endl; // 二级指针**ppi= 20
cout << "二级指针ppi= " << ppi << endl; // 二级指针ppi= 0x61ff04
int b = 10;
*ppi = &b;
cout << "改变一级指针指向 *pi = " << *pi << endl; // 改变一级指针指向 *pi = 10
cout << " pi = " << pi << endl; // pi = 0x61ff00
cout << "二级指针**ppi= " << **ppi << endl; // 二级指针**ppi= 10
cout << "二级指针ppi= " << ppi << endl; //二级指针ppi= 0x61ff04
cout << "&b = " << &b << endl; // &b = 0x61ff00
cout << *ppi << " " << pi << endl; // 0x61ff00 0x61ff00
return 0;
可以通过n级指针修改n-1级指针的指向。
指针和数组
*P++ 修改指针 ; *(p+i)不修改指针
int main()
{
int var[MAX] = {10, 100, 200};
int *ptr;
// 指针中的数组地址
ptr = var;
for (int i = 0; i < MAX; ++i)
{
cout << "addreass of var[" << i << "] = ";
cout << ptr << endl;
cout << "value of var[" << i << "] = ";
cout << *ptr++ << endl; // 修改指针
/* cout << *(ptr+i) << endl; 不修改指针 */
cout << "var[" << i << "] = " << var[i] << endl;
}
return 0;
}
addreass of var[0] = 0x61fefc
value of var[0] = 10
var[0] = 10
addreass of var[1] = 0x61ff00
value of var[1] = 100
var[1] = 100
addreass of var[2] = 0x61ff04
value of var[2] = 200
var[2] = 200
指针数组
int var[3] = {10,20,30};
int *p[3];
char *names[3] = {"10","20","30"}
函数传入指针 type xx(type xx/ type 数组名)
从函数返回指针 type * xx(){}
#include <iostream>
#include <ctime>
using namespace std;
const int MAX = 3;
int* getRandom()
{
static int r[3];
srand((unsigned)time(NULL));
for (int i = 0; i < 3;i++)
{
r[i] = rand();
cout << r[i] << endl;
}
return r;
}
int main()
{
int *p;
p = getRandom();
for (int i = 0; i < 3;i++)
{
cout << "*(p + " << i << ")" << *(p + i) << endl;
}
return 0;
}
