Mid() is a little complicated and very hard to understand if you miss that there is a semi-colon at the end of first while! This is the complete loop. It moves Source forward character by character until it reaches the end of the string or Start becomes 0.
while (*Source++ && --Start) ;When this starts, Source points to the A in "A Long string". When it exits, Source points to the L at position 2. The *Source is included so that it doesn't index past the end of a shorter string. Don't forget as well that in an &&, if the left hand is false, the right hand is not executed.
Right() actually measures the length of the string in the variable Length. This is a by product of incrementing Source until it points to the terminating zero. It's then used when decrementing Source to make sure that Source doesn't end up pointing to an address before the start of the string. The example for g tests this. It correctly prints out Short.
Lessons Learnt
- Sanity check all parameters.
- Test with extreme and idiotic cases!
- Do not allow any pointers to move before the start of a string or after the terminating zero.
On the next page : Using the mem functions in the string.h library

