# 指针

* 两种不同含义，具体看使用语境，一种为指针说明符；另一种是间接运算符或指针运算符或去内容运算符，若P为指针变量，*p则为P所指向的目标变量

int a = 5；

int *p = &a；

&p 表示指针变量P本身的地址

``````int a；
int *p；
p=&a;``````

``````int a；
int *p = &a；``````

1. 对指针赋值

int *p = &a；

2. 地址运算“&”和内容运算“*”，这两个互为逆运算

``````int a =5；
int *p=&a;

*（&a）=a``````

3. 加减

``````int a[] = "1,2,3,4,5"
int p=&a；

*p++=2；
*(p+2)=3；
*(p+3)=4；
``````

*p++相当于*（p++）；

4. 比较

(指向)整型指针：

`````` #include <iostream>
using namespace std;

int main()
{
int a = 5;
int * p = &a;
cout << "a  = " <<  a << endl
<< "&a = " << &a << endl
<< "*p = " << *p << endl
<< "p  = " << p << endl
<< "&p = " << &p << endl;

return 0;
}``````

(指向)字符型指针：

`````` #include <iostream>
using namespace std;

int main()
{
char a[] = "hello";
char *p = a;

cout << "p = " << p << endl
<<"p = " << (void *) p << endl
<< "*p = " << *p << endl;

for(int i = 0 ; i < 5; i++)
{
cout << "&a[" << i << "] = "<< (voi*)&a[i] << endl;
}
return 0;
}``````

1. 下标法 a[i];（数组名的下标变量访问）

2.指针法*（p+i）;(指针+偏移量的间接地址访问)

3.*（a+i）;（数组名作为地址值得直接地址访问）

4.p[i]; （指针作为数组名的下标变量访问）

"well",};

``````strcut info{
short a;
int b[6];
};
strcut info myinfo, *pinfo;
pinfo =&myinfo;``````

1.通过结构变量名访问 myinfo.a

2. 通过指针间接运算符访问 （*pinfo）.a

3. 通过指针和成员运算符访问 pinfo->a

int (*p)(int a ,int b);
p=max;

char a='A';
char *p=&a;
int *p1=(int *)p; //p强制转换指向整型数据指针
cout<<*p1<<endl; // 输出'A'ASCII码值65

指向整型指针的指针

`````` #include <iostream>
using namespace std;

int main()
{
int a[5] = {1, 2, 3, 4, 5};
int *p = a;
int **point = &p;

cout << "a = " << a << endl
<< "p = " << p << endl
<< "&p = " << &p << endl
<< "point = " << point << endl
<< "&point = " << &point << endl;

for (int i = 0; i < 5; i++)
{
cout << "&a[" << i << "] = " << &a] << endl;
}
return 0;
}``````

4. 指向字符型指针的指针

``````#include <iostream>
using namespace std;

int main()
{
char *a[] = {"Wel", "To", "China"};
char **p = a;
for(int i = 0; i < 3; i++)
{
for (int j = 0; j < strlen(a[i]) + 1; j++)
{
cout << a[i][j] << "\t" << (void *)&a[i][j] << endl;
}
cout << endl;
}

for (int i = 0; i < 3; i++)
{
cout << "a[" << i << "] = " << (void**) a[i] << endl
<< "&a[" << i << "] = " << &a[i] << endl;
}

cout << "p  = " << p << endl
<< "&p = " << &p << endl;
return 0;
}``````

;