Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of the return value in strrev()?

Tags:

c

What is the use of return value of strrev()? Even after reversing the string, the value will be modified in the given string.

Ex: char *strrev(char *src); Here after the string is reversed, the output will be present in the src. In that case what is the use of returning the string?

Either the return value or the string which we are passing also acts as output, that is enough. In that case what is the use of return value of strrev()?

like image 219
user1808215 Avatar asked Jun 02 '15 07:06

user1808215


1 Answers

To allow chaining.

Imagine you wanted to reverse a string, then reverse it back again (contrived example, but it makes the point). With chaining, you can do this:

strrev(strrev(charBuffer));

If sttrev returned void, you'd have to do:

strrev(charBuffer);
strrev(charBuffer);

to get the same result.

As @WernerHenze says in his comment, it also allows you to directly work with the output in function calls, like this:

printf("%s\n", strrev(charBuffer));

The possibility of chaining basically gives more flexibility to the programmer as to how the code is structured.

like image 150
Baldrick Avatar answered Oct 22 '22 06:10

Baldrick