![]() |
#2
共和国鹰派2011-12-15 00:11
|

#include <iostream>
#include <string>
using namespace std;
int main()
{
int i = 0;
char *name = new char[];
cin>>name;
char* first = new char[strlen( name ) + 1 ];
strcpy( first,name );
char p='\0';
while(*first != p)
cout<<first[i++];
cout<<first<<endl;
return 0;
}
我想的是在while那句的末尾编译器会自动添加字符'\0',因为给定的空间刚好够,strlen( name ) + 1,当输出碰到'\0'会自动停止,然而编译时后不自动停止#include <string>
using namespace std;
int main()
{
int i = 0;
char *name = new char[];
cin>>name;
char* first = new char[strlen( name ) + 1 ];
strcpy( first,name );
char p='\0';
while(*first != p)
cout<<first[i++];
cout<<first<<endl;
return 0;
}
还有个想法就是在取长度时候加一,即为strlen( name ),会不会正确执行而不越位.