- 論壇徽章:
- 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()
{
//方法一:輸出會產(chǎn)生段錯誤
char *test = "testhellloworld";
//方法二:輸出不會會產(chǎn)生段錯誤
char test[] = "testhellloworld";
//方法三:輸出不會會產(chǎn)生段錯誤
//char *test = (char *)malloc(sizeof(char)*MAXLEN);
//strcpy(test, "testhellloworld");
LoopMove(test, 3);
printf("%s\n", test);
return 0;
}
幫忙解釋一下方法一產(chǎn)生段錯誤的原因 |
|