-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex_8_4.cpp
69 lines (64 loc) · 1.78 KB
/
ex_8_4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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& arg1, char * arg2);
void show(const stringy&, int n = 1);
void show(const char *, 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,
// sets str member of beany to point to the
// new block, copies testing to new block,
// and sets ct member of beany
show(beany); // prints member string once
show(beany, 2); // prints member string twice
testing[0] = 'D';
testing[1] = 'u';
show(testing); // prints testing string once
show(testing, 3); // prints testing string thrice
show("Done!");
delete[] beany.str;
return 0;
}
void set(stringy& arg1, char* arg2) {
int size = strlen(arg2);
arg1.ct = size;
char* new_block = new char[size + 1];
arg1.str = new_block;
strcpy(new_block, arg2);
}
void show(const stringy & str, int n) {
switch (n)
{
case 1:
std::cout << "With default argument: " << std::endl;
std::cout << "Stuct's string member: " << str.str << std::endl;
break;
default:
std::cout << "With non-default argument: " << std::endl;
for (int i = 0; i != n; ++i) {
std::cout << "Stuct's string member: " << str.str << std::endl;
}
break;
}
}
void show(const char * str, int n) {
if (n == 1) {
std::cout << "With default argument: " << std::endl;
std::cout << "Array of chars: " << str << std::endl;
}
else {
std::cout << "With non-default argument: " << std::endl;
for (int i = 0; i != n; ++i) {
std::cout << "Array of chars: " << str << std::endl;
}
}
}