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.
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'
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
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.