Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
C perfomance
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="pete, post: 1698954"] The functions are the same, but I think that it would be asking a lot from a compiler, to see that. If the values of p and t were supposed to be meaningful after the loop, it would be different. The loop semantics are not the same. When t points to a zero length string, the top version will increment and the bottom one won't. In this version of strncpy, the bottom loop is my prefered method of writing a loop that will execute as many times as the intitial value of n, when I'm not counting clock ticks. But after the top loop executes, I need n to represent the number of times still left to go, so I can't write while(n-- && *s2 != '\0'), there. char *strncpy(char *s1, const char *s2, size_t n) { char *const p1 = s1; while (n != 0 && *s2 != '\0') { *s1++ = *s2++; --n; } while (n--) { *s1++ = '\0'; } return p1; } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
C perfomance
Top