Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p><code>dPtr</code> is being used to move through the string and modify it. <code>dest</code> is being kept as the beginning of that modified string, eventually the return value of the function. At the end of each loop iteration, <code>dPtr</code> will point to the end of the modified string, and used for each successive <code>strncpy</code>. Ultimately the function returns a modified string, and <code>dest</code> is needed to keep track of it's beginning.</p></li> <li><p><code>subword</code> is being null terminated presumably so that only characters in <code>subword</code> will be copied into the result in the <code>strncpy</code> within the loop, but this actually isn't necessary with the use of <code>strncpy</code>. The function moves backwards through the string looking for dashes. For each hit, <code>subword</code> is the next dash-delimited substring. The substring is a piece of memory <em>inside</em> the original string, which isn't null-terminated. If you were using <code>strcpy</code> then you'd need null-termination of <code>subword</code>, but since you're using <code>strncpy</code> the null-terminators shouldn't be necessary: just be careful with your <code>n</code> values as the <code>strncpy</code> limit. This code null-terminates <code>subword</code> <em>in place</em>, i.e. by sticking null characters inside the original string argument. Which leads to #3:</p></li> <li><p>As for undesired behavior, the function does it's work by modifying the original string argument - it's putting those null terminators for <code>subword</code> into the source string - even though it's keeping it's own buffer for the modified and returned string.</p></li> </ol> <p>Finally, the code could do without <code>dPtr</code> by using <code>strncat</code> operations rather than <code>strncpy</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload