PDA

View Full Version : returning char[] ?



jkd
09-09-2002, 04: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
09-09-2002, 01: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
09-09-2002, 02:00 PM
jkd,

Can you not declare it as:


char* someFunction(sometype somearg)

maes
09-09-2002, 02: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
09-09-2002, 02:25 PM
damn too late:o

jkd
09-09-2002, 09:57 PM
That's cool, thank you. :)