//8.8.4
#include"iostream"
#include"string"
#include"cstring"
using namespace std;
struct stringy
{
char *str;
int ct;
};
void set(stringy &str,char *testing)
{
strcpy(testing,str.str);
str.ct = strlen(testing);
}
void show(const stringy &str,int n = 1)
{
if(n <= 1)
{
cout<<str.str<<endl;
}
else
for(int i = 0;i < n;i++)
{
cout<<str.str<<endl;
}
}
void show(const char *testing,int n = 1)
{
if(n <= 1)
{
cout<<testing<<endl;
}
else
for(int i = 0;i < n;i++)
{
cout<<testing<<endl;
}
}
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany,testing);
show(beany);
show(beany,2);
testing[0] = 'D';
testing[1] = 'u';
show(testing);
show(testing,3);
show("Done!");
system("pause");
return 0;
}
因为有错,你应该用调试器运行,这样可以知道出错的代码行。看一行代码找出问题和看一整段代码找出问题的难度区别是显而易见的。
void set(stringy &str,char *testing)
{
str.ct = strlen(testing);
str.str = new char[str.ct]; //为指针分配空间才能进行字符拷贝
strcpy(str.str,testing);
}
void set(stringy &str,char *testing)
{
str.ct = strlen(testing);
str.str = testing;
}