PDA

View Full Version : returning char[] ?



jkd
Sep 9th, 2002, 03:37 AM
I'm learning C++, and am unable to do:

char[] someFunction(sometype somearg) {
// bla
}

inside the function, I return "a string literal". I suppose it is because it wants to return a reference to it? I've resorted to the apstring class, but would like to be able to use C-strings...

smeagol
Sep 9th, 2002, 12:59 PM
Are you actually declaring the function like this?:

char[] someFunction(sometype somearg)

If so, are you getting an error from the compiler that says something like "parse error before ["? Try declaring your function without the brackets. I may be wrong, but I'm not able to declare and define a function with char[], but I can with char.

Hope this helps.

Smeagol

fivesidecube
Sep 9th, 2002, 01:00 PM
jkd,

Can you not declare it as:


char* someFunction(sometype somearg)

maes
Sep 9th, 2002, 01:23 PM
you have to declare it as a pointer.


char * function ();

int main()
{
char test[100];
sprintf(test,"%s",function());
printf("%s",test);
return 0;
}

char * function ()
{
static char str[100];
strcpy(str,"testing the return of a string");
return str;
}

the static in the function is there so that the pointer will not be destroyed when the function returns

I hope this helps you a bit

maes
Sep 9th, 2002, 01:25 PM
damn too late:o

jkd
Sep 9th, 2002, 08:57 PM
That's cool, thank you. :)