Conversion between const char * and char *

(https://www.cnblogs.com/songchaohuang/articles/5591576.html)

const char*是指向常量的指针,而不是指针本身为常量,可以不被初始化.该指针可以指向常量也可以指向变量,只是从该指针的角度而言,它所指向的是常量,

Data can not be modified by the pointer pointing to it.

1.const char is not assigned directly to a char , so the compiler can not pass, reason: if you can, then by char can modify the contents of const char points, which is not allowed so char. To open up new addition space.

include <iostream>

STD namespace the using;
void main () {
const char * CPC = "ABCDE";
char * PC = new new char [100];
strcpy (PC, CPC);
COUT PC << << endl;
}
2.char to const char direct assignment on it

const char* cpc;

char* pc="abcde";

cpc=pc;

Second, the pointer constant, constant pointer

1. What is a pointer constant? I.e. Pointer Pointer constant type constant.

例:char *const name1="John";

name1="abc"; //错误,name1指针,不能变,一个指针类型的变量,存放的是地址,所以不能把'"abc"的地址赋给name1
char * name2= name1; //可以

2. What is a constant pointer? Which means constant pointer, the pointer value of the pointer may change to a constant content of the address pointer in a constant can not be changed,

例:const char *name1="John";

char s[]="abc"; name1=s; //正确,name1存放的地址可以改变
char * name2= name1; //不可以,因为name2 和 name1存放的是同一块地址,如果name2地址中的内容改了,则name1的内容也改了,那么name1就不再是指向常量的指针了。

Transfer: http://www.cnblogs.com/yc_sunniwell/archive/2010/07/13/1776613.html

Reproduced in: https: //www.jianshu.com/p/7736df723e84

Guess you like

Origin blog.csdn.net/weixin_33742618/article/details/91214604