- 论坛徽章:
- 0
|
#include <stdio.h>
#include <string.h>
#define MAXLEN 1024
void LoopMove(char *pStr, int steps)
{
int n = 0;
char tmp[MAXLEN];
n = strlen(pStr) - steps;
strcpy(tmp, pStr+n);
strcpy(tmp+steps, pStr);
*(tmp+strlen(pStr)) = '\0';//把后面的部分进行截取
memcpy(pStr, tmp, strlen(pStr));
printf("%s\n", pStr);
}
int main()
{
//方法一:输出会产生段错误
char *test = "testhellloworld";
//方法二:输出不会会产生段错误
char test[] = "testhellloworld";
//方法三:输出不会会产生段错误
//char *test = (char *)malloc(sizeof(char)*MAXLEN);
//strcpy(test, "testhellloworld");
LoopMove(test, 3);
printf("%s\n", test);
return 0;
}
帮忙解释一下方法一产生段错误的原因 |
|