// strgback.cpp  --  a function that returns a pointer to char
// C++ Primer Plus, Fifth Edition
// Chapter 7, Page 312.
// Listing 7.10
// 10 Dec, 2007.

#include <iostream>
char * buildstr(char c, int n);	   // prototype
int main()
{
	using namespace std;
	int times;
	char ch;
	
	cout << "Enter a character: ";
	cin >> ch;
	cout << "Enter an integer: ";
	cin >> times;
	char *ps = buildstr(ch, times);
	cout << ps << endl;
	delete [] ps;                   // free memory
	ps = buildstr('+', 20);     // reuse pointer
	cout << ps << "-DONE-" << ps << endl;;
	delete [] ps;
	
	cout << "\n\n...Press any key to EXIT...";
	while (cin.get() != '\n')
		;
	cin.get();
	return 0;
}
// builds a string made of n c characters
char * buildstr(char c, int n)
{
	char * pstr = new char[n+1];
	pstr[n] = '\0';        // terminate string
	while (n-- > 0)
		pstr[n] = c;   // fill rest of string
	return pstr;
}

I understand that the pstr variable is local to the buildstr function and that when the function ends that particular variable goes out of scope, can't be accessed.

From my book a paragraph explains it like so:

Note that the variable pstr is local to the buildstr function, so when that function terminates, the memory used for pstr (but not the string) is freed. But because the function returns the value of pstr, the program is able to access the new string through the ps pointer in main().

I'm confused in that the the variable pstr is lost and yet what it was pointing to or referring to, the string, can still be accessed. My question is, how can this be so?

The object pstr is an address. When you return pstr from the function, the value of that address is returned, so that in the calling function, you assign that address to ps.

If you can step through the code in a debugger, notice the value (address) pstr has, and the the value (address) ps has after the function call.

Val

In other words, the address from the new is loaded into pstr. This buffer is not local to the function, only pstr is local.
When you return, the variable pstr is destroyed, but the contents (the address of the new buffer) is returned. That buffer is not destroyed.

Thanks for replying. I've since added cout lines showing the address of both ps in main and pstr in the function. Clearly they are different addresses but contain the same values.

Is it correct to say the the value of pstr which happens to be an address is copied (transferred) to ps . ps itself resides at a different address and hence the reason for the addresses being different.

If the above is correct I'm still trying to get my head around the actual value that was passed back by reference. The address that was created by new in the function which can be determined by &pstr is where the actual value resides, whatever it is. How in code, after the function has ended, can I query or check ps to see the actual address that was returned. Below is my modified code with the debugging lines in.

// strgback-1.cpp  --  a function that returns a pointer to char
// C++ Primer Plus, Fifth Edition
// Chapter 7, Page 312.
// Listing 7.10
// 10 Dec, 2007.

#include <iostream>
char * buildstr(char c, int n);		// prototype
int main()
{
	using namespace std;
	int times;
	char ch;
	
	cout << "Enter a character: ";
	cin >> ch;
	cout << "Enter an integer: ";
	cin >> times;
	char *ps = buildstr(ch, times);
	cout << "\n&ps address = " << &ps << endl;
	cout << "*ps = " << *ps << endl;
	cout << " ps = " << ps << endl;
	
	delete [] ps;		   // free memory
	ps = buildstr('+', 20);	// reuse pointer
	cout << "\n&ps address = " << &ps << endl;
	cout << "*ps = " << *ps << endl;
	cout << " ps = " << ps << endl;

	cout << ps << "-DONE-" << ps << endl;;
	delete [] ps;
	
	cout << "\n\n...Press any key to EXIT...";
	while (cin.get() != '\n')
		;
	cin.get();
	return 0;
}
// builds a string made of n c characters
char * buildstr(char c, int n)
{
	char * pstr = new char[n+1];
	pstr[n] = '\0';		// terminate string
	while (n-- > 0)
		pstr[n] = c;	// fill rest of string
		
	std::cout << "\n&pstr address = " << &pstr << std::endl;
	std::cout << "*pstr = " << *pstr << std::endl;
	std::cout << " pstr = " << pstr << std::endl;

	return pstr;
}

Yes, I think you've got it. Your debugging statements should show you the address of the pointer variable, the address it points to (where the string is stored) and then the contents (starting at) at that memory address.

Yes, I think you've got it. Your debugging statements should show you the address of the pointer variable, the address it points to (where the string is stored) and then the contents (starting at) at that memory address.

Thank you, I think I've got it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.