When you say "returnable function", I imagine that you want to return the function, which in C means returning a pointer to the function. However, from your description, it appears that this is not so. Rather, you want to return a value from a function, and this value is a string.
Now, there is no string type in standard C. Rather, a string is a contiguous sequence of characters terminated by a null character. This sequence is stored in an array of some sort, and may be accessed via a pointer to the first character of the string (typically a pointer to the first character of the array that stores the string).
You cannot return an array from a function, but you can return a pointer. However, the pointer must point to an object that exists even after the function returns, i.e., you cannot create a non-static local array within the function, return a pointer to the first character of that array, then expect it to work.
There are a few ways around this, including:
- Have the caller pass a pointer to the first character of an array that already exists, e.g., it was declared in the caller.
- Use dynamic memory allocation.
- Use a static local array.
Using the second option here would allow the syntax that you prefer, but result in a memory leak. Using the third option would also allow the syntax that you prefer, but you may run into mistakes related to returning static local variables (e.g., you call the function more than once then find out that the string that you received from the previous call has changed). Hence, I suggest that you use the first approach, though it requires some modification to what you desire, e.g.,
Code:
char *imeino_str(char *destination, const char *source, size_t n)
{
/* ... */
return destination;
}
int main(void)
{
char result[20];
printf("%s\n", imeino_str(result, "\r\n865789024598757 OK\r\n", sizeof(result)));
return 0;
}
I have added a parameter n for the size of the destination buffer so that imeino_str can be written to avoid buffer overflow. It might not be so important given that imeino_str appears to be trimming the source string, but it is arguably still good practice.