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
|
// Soln7_02.cpp
// Add a char* member to the Sample struct in the previous exercise called sptr.
// When you fill in the data for a, dynamically create a string buffer
// initialized with "Hello World!" and make a.sptr point to it.
// Copy a into b.
#include <iostream> // For stream input/output
using std::cout;
using std::endl;
struct Sample
{
int one;
int two;
char* sptr;
};
int main()
{
Sample a;
Sample b;
char* s{ "Hello World!" };
a.one = 1;
a.two = 2;
a.sptr = new char[strlen(s)+1];
strcpy_s(a.sptr, strlen(s)+1, s);
b.one = b.two = 9999;
b.sptr = "rubbish";
cout << "a=(" << a.one << "," << a.two << "," << a.sptr << ")" << endl;
// b contains values 9999 and a pointer to "rubbish"
cout << "b=(" << b.one << "," << b.two << "," << b.sptr << ")" << endl;
b = a;
cout << "\nAfter copying a to b:" << endl;
cout << "b=(" << b.one << "," << b.two <<"," << b.sptr << ")" << endl;
a.sptr[0] = 'H'; // Change string in a
cout << "\nAfter modifying the string in a to start with 'H', b has also changed:" << endl;
cout << "a=(" << a.one << "," << a.two << "," << a.sptr << ")" << endl;
cout << "b=(" << b.one << "," << b.two << "," << b.sptr << ")" << endl;
// To avoid the two structs pointing to the same string you
// must create a copy of the string from a, and store its address in b.sptr
b.one = a.one;
b.two = a.two;
b.sptr = new char[strlen(a.sptr)+1];
strcpy_s(b.sptr, strlen(a.sptr)+1, a.sptr);
cout << "\nAfter copying members of a to b, replicating the string:" << endl;
cout << "a=(" << a.one << "," << a.two << "," << a.sptr << ")" << endl;
cout << "b=(" << b.one << "," << b.two <<"," << b.sptr << ")" << endl;
a.sptr[0] = 'h';
cout << "\nAfter modifying the string in a once more to start with 'h', b has not changed:" << endl;
cout << "a=(" << a.one << "," << a.two << "," << a.sptr << ")" << endl;
cout << "b=(" << b.one << "," << b.two << "," << b.sptr << ")" << endl;
system("pause");
return 0;
}
|