Returning Strings in a Function

Feb 7, 2013 at 10:42pm
It's been a while since I've worked with C++ and am shaky at remembering how to use arrays properly. I need to return a string from a function but for some reason it's not running correctly. Here's a simplified version.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
char* array();
int main()
{
    cout<<array();
}
char* array()
{
    char x[80]="Hello world!";
    return x;
}


This code results in some strange characters being displayed, but everything past the letter o comes up fine. Anyone know what I did wrong?
Last edited on Feb 7, 2013 at 10:42pm
Feb 7, 2013 at 11:00pm
closed account (Dy7SLyTq)
as a side note if your using the array for strings, instead do #include <string>
which gives you access to the string data type. second do:
char x[] = "Hello, world!";
return x;

if you have [int] = "string"

and int > len of string, the remaining spots are initialized to '\0'
Feb 7, 2013 at 11:04pm
Okay thanks, though I find it odd that putting [int] would affect the first few characters.
Feb 7, 2013 at 11:05pm
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

char const*array();

int main()
{
    std::cout << array();
}
char const*array()
{
    return "Hello world!";
}
Last edited on Feb 7, 2013 at 11:05pm
Feb 8, 2013 at 12:35am
closed account (Dy7SLyTq)
@LB: i thought of that but he wants to return an array not a string literal
Feb 8, 2013 at 12:41am
If he wants to return an array he had better stop using C++

I agree that he should use std::vector
Feb 8, 2013 at 12:48am
While the above helped for returning an array declared within the function, I'm having the same problem with an array assembled in the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
char* digits();
int main()
{
    cout<<digits();
    system("pause");
    return 0;
}
char* digits()
{
    char x[80];
    int cnt;
    for(cnt=0;cnt<10;cnt++)
    x[cnt]=(char)cnt+48;
    x[10]='\0';
    return x;
}


It's supposed to list digits 0 through 9 but instead outputs some odd characters and then 456789. I'm not really sure why.
Last edited on Feb 8, 2013 at 12:49am
Feb 8, 2013 at 1:32am
I'm pretty sure it's outputting those odd characters and numbers because you've initialized digits() as a pointer with *, it's displaying the member location of that function. If you wanted to output digits, you would use cout<<*digits . However, I'm not sure if you can return an array, as I am very new at C++ - But if you directly cout x array via the digits function instead of trying to return the array - it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char digits()
{
    char x[80];
    int cnt;
    for(cnt=0;cnt<10;cnt++)
    x[cnt]=(char)cnt+48;
    x[10]='\0';
	    cout<<x<<endl;

}

int main()
{
    cout<<digits();
    system("pause");
    return 0;
}


EDIT ** Sorry - I re-read this thread and apparently you are after returning the array from the function return
Last edited on Feb 8, 2013 at 1:38am
Feb 8, 2013 at 1:37am
You're trying to return a pointer to storage that has gone out of scope.
When digits exits, the storage for x is no longer valid.

Try this instead:
1
2
3
4
5
6
7
8
9
#include <string>

string digits()
{  string x; 
    int cnt;
    for(cnt=0;cnt<10;cnt++)
        x += (char)cnt+48;
    return x;
}

Feb 8, 2013 at 4:09am
Ah I get it, I had to initialize the array outside the function, pass it in, then return it. This logic gets really complicated. Thanks for the help.
Feb 8, 2013 at 6:00am
closed account (S6k9GNh0)
You can return an array as long as you know how that array's memory is managed. You must also know the length of that array. This is sometimes done in C libraries with a init/quit type of library.

AbstractionAnon, understand that that can be a relatively heavy operation.
Last edited on Feb 8, 2013 at 6:07am
Topic archived. No new replies allowed.