天天看点

c++指针总结

什么叫指针?

指针某一变量或函数的内存地址,是一个无符号整数,它是以系统寻址范围为取值范围,32位,4字节。

指针变量:

存放地址的变量,在C++中,指针变量只有有了明确的指向才有意义。

指针类型

int*ptr; //指向int类型的指针变量

char*ptr;

float*ptr;

指针的指针:

char*a[]={"hello","the","world"};

char**p=a;

p++;

cout<<*p<<endl;          //输出the

函数指针:

指向某一函数的指针,可以通过调用该指针来调用函数。

例子:

int max(int ,int);

int (*f)(int int)=&max;

d=(*f((*f)(a,b),c));

指针数组:

指向某一种类型的一组指针(每个数组变量里面存放的是地址)

int*ptr[10];

数组指针:

指向某一类型数组的一个指针

int v[2][10]={{1,2,3,4,5,6,7,8,9,10},{11,12,13,14,15,16,17,18,19,20}};

int (*a)[10]=v;//数组指针

cout<<**a<<endl;         //输出1

cout<<**(a+1)<<endl;     //输出11

cout<<*(*a+1)<<endl;    //输出2

cout<<*(a[0]+1)<<endl;  //输出2

cout<<*(a[1]+1)<<endl;  //输出12

cout<<a[0]<<endl;     //输出v[0]首地址

cout<<a[1]<<endl;     //输出v[1]首地址

int*p与(int*)p的区别

int*p:p指向×××的指针变量

(int*)p:将p类型强制转换为×××的指针

数组名相当于指针,&数组名相当于双指针

int a[]={{1,2,3,4,5};

int*ptr=(int*)(&a+1);//二维数组,整体加一行

printf("%d%d",*(a+1),*(ptr-1));//输出25

char*str="helloworld"与char str[]="helloworld"的区别

char*str="helloworld":分配全局数组,共享存储区

char str[]="helloworld":分配局部数组

继续阅读