[ALIB2-EXAM]有关memmove时的char *p=" "和char p[]=" "的区别

char *p=" "中是const的字符串,char p[]=" "不是const的字符串,前者不允许修改,修改导致运行时错误。http://baike.baidu.com/view/1026882.htm 百度百科中memmove举出的这个例子就犯了这个错误。

 

/***************************************************************************** * * DESCRIPTION: 2009年10月17日 * 一道关于char *str=" "和char str[]="";区别的题 * 前者是const的常量内存空间(不能修改),而后者不是(可以修改) * http://baike.baidu.com/view/1026882.htm 就犯了这个错误。 * * (主要考察memmove) * * AUTHOR: Neesky * * DATE:2009-10-17 * *****************************************************************************/ /** include files **/ #include <stdio.h> #include <iostream> using namespace std; int main (int argc, char *argv[]) { /*(1)Crash,for const string, cannot be changed*/ //char* str="Golden Global View"; /*(2)Then OK, for not const string*/ char str[]="Golden Global View"; cout<<strlen(str)<<": "<<str<<endl; memmove(str,str+1,strlen(str)); cout<<strlen(str)<<": "<<str<<endl; return(0); }  

输出:

18: Golden Global View
17: olden Global View

 

 

你可能感兴趣的:([ALIB2-EXAM]有关memmove时的char *p=" "和char p[]=" "的区别)