Here's the same program, with altered code, as per suggestion by AndrewHunter and tabstop.
Now, here's why I'm still puzzled: when I run the program, again, everything seems to do what it's supposed to do, up until after the last function is called in main. I prove this by adding cin.get() to pause after the last function.
I've added testing the entire way, to prove to myself that each "copying" step of the set() function is successful. The tests seem to indicate that this is the case.
So seriously... I realize my novice status dictates automatically that my code will be sub par, but can anyone actually explain why the program still errors out at the very end? I just want to understand what I'm doing wrong. Try running the program and see... and for anyone who missed the last thread, yes, I realize I'm using C code in C++. Regardless of purity of standard, the program should still operate without the return value failure (that's why I'm trying to figure out exactly what I'm doing wrong).
Code:#include <iostream> using namespace std; #include <cstring> struct stringy { char * str; int ct; }; void set(stringy & x, char * y); void show(stringy & a, int b = 1); void show(const char * c, int d = 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!"); cout << "testing[14]: " << testing[14] << "\n"; // tests original string index cout << "beany.str[14]: " << beany.str[14] << "\n"; // tests copied string index cout << "testing[6]: " << testing[6] << "\n"; // test of a different index, just to be sure cout << "beany.str[6]: " << beany.str[6] << "\n"; // test of a different index, just to be sure cout << "testing: " << testing << "\n"; // verifying entire string still intact cout << "beany.str: " << beany.str << "\n"; // verifying entire copied string is still intact cin.get(); /// this is here to prove program runs smoothly until return fails!!! return 0; } void set(stringy & x, char * y) { size_t length = strlen(y) + 1; char * space = new char[length]; cout << "y[14]: " << y[14] << "\n"; // verifying testing string with random index cout << "space[14]: " << space[14] << "\n"; // verifying space currently has garbage values cout << "x.str[14]: " << x.str[14] << "\n"; // verifying sure beany.str currently has garbage values space[length] = '\0'; for (int i = 0; i < length; i++) space[i] = y[i]; cout << "y[14]: " << y[14] << "\n"; // verifying testing string with random index cout << "space[14]: " << space[14] << "\n"; // verifying space received proper values cout << "x.str[14]: " << x.str[14] << "\n"; // verifying sure beany.str currently has garbage values space[length] = '\0'; strcpy(x.str, space); cout << "y[14]: " << y[14] << "\n"; // verifying testing string with random index cout << "space[14]: " << space[14] << "\n"; // verifying space received proper values cout << "x.str[14]: " << x.str[14] << "\n"; // veryfying beany.str received proper values cout << "testing: " << y << "\n"; // verifying testing string is still intact cout << "space: " << space << "\n"; // veriyfing space string was copied correctly cout << "x.str: " << x.str << "\n"; // verifying beany.str string was copied correctly delete space; } void show(stringy & a, int b) { for (int i = 0; i < b; i++) cout << a.str << "\n"; } void show(const char * c, int d) { for (int i = 0; i < d; i++) cout << c << "\n"; }