const char *转化为char *的方法

直接将const char *赋值给char *是错误的,编译器不允许这种操作。

#include "stdio.h" #include "string.h" int main() { //方式1 strcpy //方式1.1 //const char *cpCh = "const char"; //char ch[64]={0}; //char *pCh = ch; //strcpy(pCh, cpCh); //方式1.2 const char *cpCh = "const char"; int nLen = strlen(cpCh); char *pCh = new char[nLen + 1]; strcpy(pCh, cpCh); //方式2 const_cast<>() //const char *cpCh = "const char"; //char *pCh = const_cast(cpCh);//DO not forget '()'

printf("%s", pCh); return 0; }

 

你可能感兴趣的:(const char *转化为char *的方法)