Skip to content

Instantly share code, notes, and snippets.

@mahata
Created September 28, 2011 20:12
Show Gist options
  • Save mahata/1249120 to your computer and use it in GitHub Desktop.
Save mahata/1249120 to your computer and use it in GitHub Desktop.
C++ Primer 8.4
#include <iostream>
using namespace std;
#include <cstring> // for strlen(), strcpy()
struct stringy {
char * str;
int ct;
};
void set(stringy & stringy_ref, char * cstring);
void show(stringy & stringy_ref, int times = 1);
void show(const char * cstring, int times = 1);
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!");
delete [] beany.str;
return 0;
}
void set(stringy & stringy_ref, char * cstring)
{
stringy_ref.ct = strlen(cstring);
stringy_ref.str = new char[stringy_ref.ct + 1];
strcpy(stringy_ref.str, cstring);
}
void show(stringy & stringy_ref, int times)
{
for (int i = 0; i < times; i++) {
cout << stringy_ref.str << endl;
}
}
void show(const char * cstring, int times)
{
for (int i = 0; i < times; i++) {
cout << cstring << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment