const char*
各种指针类型解释:
const int n=5; //same as below
int const m=10;
const int *p; //same as below const (int) * p
int const *q; // (int) const *p
char ** p1; // pointer to pointer to char
const char **p2;// pointer to pointer to const char
char * const * p3;// pointer to const pointer to char
const char * const * p4;// pointer to const pointer to const
char char ** const p5;// const pointer to pointer to char
const char ** const p6;// const pointer to pointer to const char
char * const * const p7;// const pointer to const pointer to char
const char * const * const p8;// const pointer to const pointer to const char
实例:
void foo(const char **pp)
{
//*pp = NULL;
//*pp = "Hello world";
char* p = (char*)malloc(10);
//*pp = (char*)malloc(10);
*pp = p;
//strcpy(*pp,"skdsl");
//(*pp)[1]='x';
}
int main()
{
const char* p = "hello";
p = NULL;
//foo(&p);
//p[1] = 'x';
return 0;
}
const char* p = "hello";
解释为“p是一个指针,指向const char”,即p的指是可以修改的,可以指向另外的地址;而p指向的内容是const char,是不能修改的,所以只要是修改(不管是直接还是间接)p指向的内容,都是不允许的。
所以main中的 p[1] = 'x',是错误的。foo()中的*pp等价于p,所以*pp = "Hello world"是可以的(*pp(p)指向了“hello world”的地址),而strcpy(*pp,"sdkds")是对*pp指向的内容(const char)赋值,这是不行的。
阅读(740) | 评论(0) | 转发(0) |