Program Notes
The program in Listing 8.9 uses new
to create a new string for holding the selected characters. One awkward possibility is that an uncooperative user may request a negative number of characters. In that case, the function sets the character count to 0
and eventually returns the null string. Another awkward possibility is that an irresponsible user may request more characters than the string contains. The function protects against this by using a combined test:
i < n && str[i]
The i < n
test stops the loop after n
characters have been copied. The second part of the test, the expression str[i]
, is the code for the character about to be copied. If the loop reaches the null character, the code is 0
, and the loop terminates. The final ...
Get C++ Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.