I'm using C language. I have a function called Reversal(char string[], char result_string[]). I need to reverse the alphabets in string and store the reversed string in result_string. Reversal has to return result_string to my main function. How do I make Reversal return an array? I tried using a pointer pointing to result_string, and returned the pointer, but only the first character, in result_string[0], got returned to main and not the whole array.
Click to view my code:
http://i34.photobucket.com/albums/d128/d...
I googled and searched yahoo answers and saw solutions involving something called malloc, and other complicated stuff. Is there a simpler way to do this?
I'm only in my first month into programming. So hopefully you guys can help with some simpler solutions please!
How to return a char array from function?
The standard and safest way to do this is to pass a pointer to an array along with its size to the function. The function then fills in the array.
The library function strnprintf is an example.
Your main would look like:
int main()
{
char myreturnarray[128];
fillinthearray(myreturnarry, sizeof(myreturnarray));
printf("array is now %26lt;%s%26gt;\n", myreturnarray);
return 0;
}
You would have a function that returns the characters:
void fillinthearray(char *thearray, int arraysize)
{
snprintf(thearray, arraysize, "filling in the array of size %d", arraysize);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment