#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include <string.h>
void LoopMove(char *pStr, int steps)
{
int n = strlen(pStr) - steps;
char temp[200];
strcpy(temp, pStr+n);
strcpy(temp+steps,pStr);
*(temp + strlen(pStr)) = '\0';
strcpy(pStr,temp);
}
int main(int argc, char** argv[])
{
//char ppstr[10] = "123456789";
char* ppstr = "123456789";
LoopMove(ppstr, 3);
printf("%s", ppstr);
system("pause");
return 0;
}
这里如果把ppstr声明为数组类型,则执行没有错误,如果把ppstr声明为指针类型,则执行到strcpy(pStr,temp) 这句话时会出现中断,那么为什么会出现这种情况呢?
</pre><pre class="cpp" name="code">1,这这道题不仅是考察strcpy,strlen函数的使用,还考察了,字符串指针和字符数组的区别
2,区别: char ppstr[10] = "123456789"; //"123456789"存储在栈中,值可以被修改
char* ppstr = "123456789";; //"123456789"存储在静态常量区,ppstr 指向"123456789",即是ppstr保存的是"123456789"在常量区的地址,其值不能被修改。
所以,当声明字符串指针并初始化后,在函数LoopMove中使用strcpy(pStr,temp) 会改变ppstr的内容,所以会报错。