70,018
社区成员




void LoopMove (char *pStr, int steps )
{
int n = strlen( pStr ) - steps;
char tmp[100];
strcpy ( tmp, pStr + n );
strcpy ( tmp + steps, pStr);
*( tmp + strlen (pStr)) ='\0';
strcpy( pStr, tmp );
}
int _tmain(int argc, _TCHAR* argv[])
{
int i=2;
//char aa[10] = "abcdefgh";
char *aa = "abcdefgh";
LoopMove(aa,i);
cout<<"aa[]="<<aa<<endl;
system("pause");
return 0;
}
现在认真考虑才知道 其中的区别
1、char aa[10] = "abcdefgh";
2、char *aa = "abcdefgh";
1 数组变量是在 数据存储区
2 指针aa 指向的地址数据是常量 数据不可以修改
不知道我这样理解正确不的
void LoopMove (char *pStr, int steps )
{
int n = strlen( pStr ) - steps;
char tmp[100];
strcpy ( tmp, pStr + n );
strcpy ( tmp + steps, pStr);
*( tmp + strlen (pStr)) ='\0';
strcpy( pStr, tmp );
}
int _tmain(int argc, _TCHAR* argv[])
{
int i=2;
//char aa[10] = "abcdefgh";
char *aa = "abcdefgh";
LoopMove(aa,i);
cout<<"aa[]="<<aa<<endl;
system("pause");
return 0;
}
现在认真考虑才知道 其中的区别
1、char aa[10] = "abcdefgh";
2、char *aa = "abcdefgh";
1 数组变量是在 数据存储区
2 指针aa 指向的地址数据是常量 数据不可以修改
不知道我这样理解正确不的
void LoopMove (char *pStr, int steps )
{
int n = strlen( pStr ) - steps;
char tmp[10];
strcpy ( tmp, pStr + n );
strcpy ( tmp + steps, pStr); --》越界了。 steps==2时,这里执行完后tmp为ghabcdefgh 共10个字符,最后还加上一个'\0',11个,超过了tmp[10]。把tmp数组改大点再试试
*( tmp + strlen (pStr)) ='\0';
strcpy( pStr, tmp );
}
void LoopMove (char *pStr, int steps )
{
int n = strlen( pStr ) - steps;
char tmp[10];
strcpy ( tmp, pStr + n );
strcpy ( tmp + steps, pStr);
*( tmp + strlen (pStr)) ='\0';
strcpy( pStr, tmp );
}
int _tmain(int argc, _TCHAR* argv[])
{
int i=2;
char aa[10] = "abcdefgh";
LoopMove(aa,i);
cout<<"aa[]"<<aa<<endl;
system("pause");
return 0;
}