The cost of doing this is linear in the length of the first string, s1. In the strcat call, determining the position of the last character involves traversing the characters just copied to d1. For simplicity, the examples that follow use d instead of storing the return value in d1 and using it. Strcat (d1, s2) // pass 2 over the copy of s1 in dīecause strcpy returns the value of its first argument, d, the value of d1 is the same as d. Let's break up the calls into two statements.Ĭhar *d1 = strcpy (d, s1) // pass 1 over s1 To perform the concatenation, one pass over s1 and one pass over s2 is all that is necessary in addition to the corresponding pass over d that happens at the same time, but the call above makes two passes over s1. The idiomatic (though far from ideal) way to append two strings is by calling the strcpy and strcat functions as follows. This inefficiency can be illustrated on an example concatenating two strings, s1 and s2, into the destination buffer d. However, by returning a pointer to the first character rather than the last (or one just past it), the position of the NUL character is lost and must be computed again when it's needed. The pointers point either at or just past the terminating NUL ('\0') character that the functions (with the exception of strncpy) append to the destination. The functions traverse the source and destination sequences and obtain the pointers to the end of both. But, as mentioned above, having the functions return the destination pointer leads to the operation being significantly less than optimally efficient. The optimal complexity of concatenating two or more strings is linear in the number of characters. The functions could have just as easily, and as it turns out, far more usefully, been defined to return a pointer to the last copied character, or just past it. Even though all four functions were used in the implementation of UNIX, some extensively, none of their calls made use of their return value. The first subset of the functions was introduced in the Seventh Edition of UNIX in 1979 and consisted of strcat, strncat, strcpy, and strncpy. The design of returning the functions' first argument is sometimes questioned by users wondering about its purpose–see for example strcpy() return value, or C: Why does strcpy return its argument? The simple answer is that it's due to a historical accident. They should not be viewed as recommended practice and may contain subtle bugs.
#TODD C BUILDER CODE#
The code examples shown in this article are for illustration only. The choice of the return value is a source of inefficiency that is the subject of this article. Both sets of functions copy characters from one object to another, and both return their first argument: a pointer to the beginning of the destination object. This is our passion, our pleasure and our promise to you.Among the most heavily used string handling functions declared in the standard C header are those that copy and concatenate strings. Our goal is not to be the biggest, just the very best at what we do. At Todd Glowka Builder, Inc., our mission is to build homes of exceptional value. We limit the number of homes we build, allowing us to maintain quality in each and every home we construct. Our experience, reputation and attention to detail assure that your building expectations will be surpassed.
#TODD C BUILDER PROFESSIONAL#
Our team of professional architects, structural engineers, designers, interior decorators, title companies and mortgage companies ensure your experience is a pleasant process. Our personal approach to homebuilding underscores constant communication and teamwork between client and builder, from concept to completion. We specialize in estate lots, private ranches and golf course communities. Whether you've already designed your dream home or have no idea where to start, we can help.We’re Todd Glowka Builder, Inc., a custom home builder specializing in building custom homes in northern San Antonio, Boerne and the surrounding Texas Hill Country.