Hi all.

I've been reading 'Beginning Visual C++ 6' from Wrox, and one of the exercises (chapter 7 if you're interested) is based on this:

Create a struct, X, with two int members and a char* member called sptr. declare 2 variables of type X, a and b. Dynamically create a string buffer and make a.sptr point to it. Assign a to b, and then output the string b.sptr points to. Change the string via a.sptr, then output it through b.sptr. What happens and why?

Well, that's a summary of what it says. First of all I get errors when I run my code:

Code:
// Ex7-1
// Struct assignment

#include <iostream>
#include <cstdlib>

using namespace std;

struct X
{	int x;
	int y;
	char* sptr;
};

void main(...)
{	X a, b;
	a.x = 1;
	a.y = 2;
	
	if(!(a.sptr = new char[14]))
	{	cout << endl <<"Error allocating memory" << endl;
		exit(1);
	}

	a.sptr = "String Buffer";

	cout << endl << a.sptr << endl;
	
	b = a;

	cout << endl << a.sptr << endl;
	cout << endl << b.sptr << endl;

	a.sptr = "New String___";

	cout << endl << a.sptr << endl;
	cout << endl << b.sptr << endl;

	delete [] a.sptr;
	return;
}
The errors I get are "Debug assertion failed!"

Although I get errors, the code still executes to the end, and I get this output:

Code:
String Buffer

String Buffer

String Buffer

New String___

String Buffer
So I can see that the data seemingly appears different in a to b.

Is it because two strings are being created, one in each variable? Hmm... it's confusing, the way that char* variables work. When I use 'a.sptr' as an expression, is it a string literal or is it an address? The same goes for char[] arrays. I'm confused, could someone please clear this up for me?