Created
September 28, 2011 20:12
-
-
Save mahata/1249120 to your computer and use it in GitHub Desktop.
C++ Primer 8.4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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