Helpful Information
 
 
Category: Computer Programming
returning char[] ?

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

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

jkd,

Can you not declare it as:


char* someFunction(sometype somearg)

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

damn too late:o

That's cool, thank you. :)










privacy (GDPR)