R
Richard Bos
Eric Sosman said:This is all right, but I dislike initializing variables with
values that will never be used: Your function will either set lvar
to something else, or will return without ever touching it, so this
initial value is useless. Yes, I know, some people think it's a
good idea to initialize every variable, especially every pointer
variable, but those people are mistaken. So Say I, The Authority
On Whose Word You Should Always Rely.
Regardless of whether you agree with Eric's comments above, at least
_never_ pre-initialise one of your objects, but not the others.
Whichever style you choose, be consistent with it.
Here's the reason for using a duplicate. But think a moment:
You know where the substring's first character is (at `t'), and you
know where the terminator is (at `p'), so you also know how long the
substring is (`p - t' characters). Instead of storing a '\0' just
so strcat() can find it again, why not just memcpy() the known number
of characters starting at `t'?
Or strncat(), which both copies only N characters _and_ appends the null
terminator.
Richard