本文共 1184 字,大约阅读时间需要 3 分钟。
在C语言中,const和数据类型名的顺序并不影响,只要*和变量名相连即可。例如:
int const *p; const int*p2;
以上代码中,*p始终与const和数据类型名相连。 const型指针变量的值是不可改变的,但它指向的内存空间内容可以是变化的。例如:
int *const p1; //const指针变量的声明
int a=1; int b=2; int *const p=&a;
*p=b; //相当于p=b,但p不能再被赋值为其他地址
这种指针变量同时指向const类型的空间。例如:
const int* const p1;
int a=1; const int *const p=&a;
*p=520; //错误,p指向的内存空间内容是不可变的
指针变量可以作为函数的形参,用于地址传递。例如:
int ceshi(int* p3);
int a=2; int *p1=&a; printf("普通变量a的地址:%p\n",&a);
ceshi(p1);
函数返回值可以是指针类型。例如:
int* max(int a,int b,int c);
int d=0,e=0,f=0; int*p1=NULL; p1=max(&d,&e,&f);
函数型指针用于存储函数的地址。例如:
int add(const int a,const int b); int(*func)(const int a,const int b); func=add;
printf("3+4=%d\n",func(3,4));
void型指针可以指向任意类型的内存空间。例如:
void* p;
转载地址:http://wzzwz.baihongyu.com/