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

What is the use of strrev() return value? Even after changing the line, the value will be changed in this line.

Example: char *strrev(char *src); Here, after the line is inverted, the output will be present in src. In this case, why use a string?

Either the return value or the string that we pass also acts as output, this is enough. In this case, use the strrev() return value?

+5
source share
2 answers

To provide the chain.

Imagine you want to change a line and then cancel it back (a far-fetched example, but it makes a point). With a chain, you can do this:

 strrev(strrev(charBuffer)); 

If sttrev returned void , you will need:

 strrev(charBuffer); strrev(charBuffer); 

to get the same result.

As @WernerHenze notes in his comment, it also allows you to directly work with output in function calls, for example:

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

The ability to chain basically gives the programmer great flexibility regarding how the code is structured.

+5
source

The chain makes more sense, but it can also be used in the unlikely micro-optimization. During a call, you can avoid storing a temporary value on the stack. For instance:.

 char *fn(char *s) { strrev(s); strlwr(s); return strstr(s, "secret"); } 

For calls to strrev() and strlwr() you need to keep a safe copy of s for later use. You can delete this register using:

 char *fn(char *s) { s = strrev(s); s = strlwr(s); return strstr(s, "secret"); } 

According to the response to the chain, you can also use:

 char *fn(char *s) { return strstr(strlwr(strrev(s)), "secret"); } 

But this can be a problem in more complex code.

I just tried and he excluded three mov instructions. Yay !!

0
source

All Articles