void指针:主要是便于传递不同类型的参数
const指针:
const char *p :const 在* 的左边表示指向一个常量指针:表示指向的内容不可变(*p不能变,p可以改变)
int a = 3;
int b = 4;
const int *p = &a;
p=&b;允许;但是*p = 5是不允许的;
char * const p:const在* 的右边表示一个指针常量
int a = 3;
int b = 4;
char * const p = &a;
p=&b;//这是不允许的,但*p = 5;这是允许的;
const char* const p:以上两种情况一起;即无论是指针常量还是常量指针都不允许改变;
#include<iostream> using namespace std; int main() { char *a = "godod"; char *b = "morening"; const char *p = "helo"; p = a; p = b; cout<<"p = "<<p<<" "; int c = 2; int * const q = &c; *q = 4; cout<<"*q = "<<*q<<" "; }
结果:
p = morening
*q = 4