Return a Pointer

I'm stuck in here, just need some tips to complete this.

string *fullName = new string[3];
allocates an array dynamically, that is, while the program is running. Suppose you created a static array instead as in
string fullName[3];

The rest of the code stays the same. Will the program still work? Why or why not?
This is what my teacher wants from me.

Thanks

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
/* Return a Pointer from a function
	Create a program with a function that returns a pointer to a person's
	full name.
*/
#include <iostream>
#include <string>
using namespace std;

string *getFullName();

int main()
{
	// Write the code to call the function and get the returned pointer
	
	cout << "\nThe name is " << name[0] << " " << name[1] << " " << name[2] << endl;
}

string *getFullName()
{
	string *fullName = new string[3];  // a dynamically allocated string array to hold the 3 names
	cout << "Enter you first name: ";
	getline();  // fill in the code to get the first name
	cout << "Enter you middle name: ";
	getline();  // fill in the code to get the first name
	cout << "Enter you last name: ";
	getline();  // fill in the code to get the first name
	
	// add the return statement
	
}[code]
[/code]
Last edited on
Why or why not - be specific!


Who are you, again? Since you want to be specific,
Use Code Tags (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your
post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <>
formatting button.

PS: Edit your initial post, you may not mean it, but you are coming up as rude with the exclamation point.

Last edited on
I just copy and paste it, i didint mean to be bossy
No worries. I figure you didn't mean to come up like bossy.

If the function code is like below,

1
2
3
4
5
6
7
8
9
10
11
string *getFullName()
{
string fullName[3];
cout << "Enter your first name: ";
getline(cin, fullName[0]);
cout << "Enter your middle name: ";
getline(cin, fullName[1]);
cout << "Enter your last name: ";
getline(cin, fullName[2]);
return fullName;
}

The function returns a pointer to an array that no longer exists. Because the fullName array is defined locally, it is destroyed when the function terminates. Attempting to use the pointer will result in erroneous and unpredictable results.

If it is like below:

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
/* Return a Pointer from a function
Create a program with a function that returns a pointer to a person's
full name.
*/
#include <iostream>
#include <string>
using namespace std;

string *getFullName();

int main()
{
    // Write the code to call the function and get the returned pointer.
    string *name = nullptr;
    name = getFullName();

    delete[] name;
    name = nullptr;

    return 0;
    
}

string *getFullName()
{
    string *fullName = new string[3];
    cout << "Enter your first name: ";
    getline(cin, fullName[0]);
    cout << "Enter your middle name: ";
    getline(cin, fullName[1]);
    cout << "Enter your last name: ";
    getline(cin, fullName[2]);

    return fullName;
}


Then it will work. This function uses the new operator to allocate a section of memory. This memory will remain allocated until the delete operator is used or the program ends, so it’s safe to return a pointer to it.

I hope I understood the question and helped.
thank you very much chikofeo
Topic archived. No new replies allowed.