July 28th, 2013, 06:12 AM
Shortened version of STRCPY()-- where does it test for NULL ?
I saw this version of strcpy in a C book:
void strcpy(char *s, char *t)
How does it know when to stop the copying?
There is another version that is almost identical, where they added ((*s++=*t++)!='\0') to the while loop and i quote them: "the comparison against '\0' is redundant".
Also, what is the exact priority of the operators in the second case:
Thanks in advance.
July 28th, 2013, 06:59 AM
In C assignment is an expression rather than a statement. That is to say it has a result value like any other expression.
The result of an assignment expression is the value of the right-hand-side. It is this attribute that allows expresions such as:
In your case the loop terminates when *t++ is zero - i.e. when the next character in the source string is nul.
a = b = c = d = 0 ;
The mere fact that you have to ask is one good reason not to write expressions like that. It may be "clever" but it is not "smart". It has its place perhaps in library code but clarity is not served by such expressions.
Originally Posted by C learner
The honest answer is I don't know, but to find out would be simple enough but not effective use of my time. It is sufficient to realise that the the rules are arcane and often undefined, so the truly smart programmer avoids the issue altogether and never uses ++ or -- (post or pre) in an expression where other operators are involved. So essentially restricting use to:
Brian Kernighan said this:
Comments on this post
Last edited by clifford; July 28th, 2013 at 07:02 AM.
July 29th, 2013, 05:51 AM