Hi:
is it possible to return a string from a C function (such as an array: x[10])?
Also, is it possible to return a structure from a C function?
Thanks in advance..
Hi:
is it possible to return a string from a C function (such as an array: x[10])?
Also, is it possible to return a structure from a C function?
Thanks in advance..
Yes it's possible to return a string from a function. Simply declare the function as a char * which would allow you to return a pointer to the desired string. It's been a long time since I've had to return a string from a function so I believe you'd have to either declare the string inside the function as a static char or use dynamic memory allocation in order to have it work (else the data inside the string could be lost as soon as the function ends). You can do the same with a structure by declaring the function as your structure type, but make sure the function is returning a pointer to the structure rather then just returning the structure.
Hope this helps,
Tyouk
Try this:
Code:char* return_my_name() { return "zeeshan"; } main() { printf("%s",return_my_name()); }