如题
#include<iostream>
using namespace std;
#include<cstring> // for strlen(), strcpy()
struct stringy
{
char *str; // points to a string
int ct; // length of string (not counting '\0')
};
// prototypes for set(), show(), and show() go here
void set(stringy & beany, const char *str);
void show(const stringy & beany, const int n = 1);
void show(const char * beany, const int n = 1);
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany, testing); // first argument is a reference
// allocates space to hold copy of testing
// set str member of beany to point to the
// new block, copies testing to new block,
// and set ct member of beany
show(beany); // prints member string once
show(beany, 2); // prints member string twice
testing[0] = 'p';
testing[1] = 'u';
show(testing); // prints testing string once
show(testing, 3); // prints testing string theice
show("Done!");
cin.get();
return 0;
}
void set(stringy & beany, const char * str)
{
beany.ct = strlen(str);
beany.str = new char[beany.ct + 1];
strcpy(beany.str, str);
}
void show(const stringy & beany, const int n)
{
for (int i = 0; i < n; i++)
{
cout << beany.str << endl;
}
}
void show(const char * beany, const int n)
{
for (int i = 0; i < n; i++)
{
cout << beany << endl;
}
}
 第八章 第四题&spm=1001.2101.3001.5002&articleId=74059042&d=1&t=3&u=922282c75ece4b879b0fc8be71c0de30)
2522

被折叠的 条评论
为什么被折叠?



