D
dominant
What this function does?
void test_function(char *s, char *t) {
while(*s++=*t++)
;
}
void test_function(char *s, char *t) {
while(*s++=*t++)
;
}
dominant said:What this function does?
void test_function(char *s, char *t) {
while(*s++=*t++)
;
}
dominant said:What this function does?
void test_function(char *s, char *t) {
while(*s++=*t++)
;
}
dominant said:What this function does?
void test_function(char *s, char *t) {
while(*s++=*t++);
This is terrible C style. The equals sign is not a equality comparison, asdominant said:void test_function(char *s, char *t)
while(*s++=*t++)
;
}
Malcolm said:The combination of three quirky C features in one short loop makes this code
very difficult to read and debug.
Thomas said:[snip]Malcolm said:"dominant" <[email protected]> wrote in messageThe combination of three quirky C features
in one short loop makes this code
very difficult to read and debug.
Given the fact that it is a well known and accepted idiom
it is neither hard to read or hard to debug.
"... the idiom should be mastered,
because you will see it frequently in C programs."
of this paradigm that drew me to C in the first place.It shouldn't do. Programs are designed to present a series of instructionsPeter Nilsson said:I can't speak for anyone else, but it was the extraordinary succinctness >
Malcolm said:It shouldn't do. Programs are designed to present a series of instructions
to a computer, but they must also present code to another human programmer.
Generally the cost of a programmmer's time reading and understanding code
far exceeds the cost of computer time executing it.
There are exceptions,
such as the inner loops of time-critical programs like games, but as a rule
code should be written so that it is as easy as possible to understand.
You can assume that your reader knows the C basics,
and knows how to
program, but you shouldn't necessarily assume that he is comfortable with C
arcana. In particular constructions such as
while(a = b)
which suggest the wrong meaning should be avoided.
What is difficult to understand about...?
while (*s++ = *t++)
;
Thomas Stegen said:Given the fact that it is a well known and accepted idiom
it is neither hard to read or hard to debug. First time I saw
it it took me about 1 minute to figure out what this was
doing.
Jarno said:First, there's an off by one error. Second, it doesn't terminate
the destination string.
No there isn't (an error). Yes it does (terminate). All assuming
the source is a legitimate string and source and destination do
not overlap.
Jarno A Wuolijoki said:First, there's an off by one error.
Where?
Second, it doesn't terminate the destination string.
![]()
No there isn't (an error). Yes it does (terminate).
Jarno A Wuolijoki said:And that's supposed to be easy to understand?
CBFalconer said:No there isn't (an error). Yes it does (terminate). All assuming
the source is a legitimate string and source and destination do
not overlap.
Exactly. We have a construct that is supposedly "easy to understand",Jarno A Wuolijoki said:And that's supposed to be easy to understand?
Malcolm said:Even an experienced C programmer, though he will not
misinterpret it, will probably take longer to read and understand the
expression and verify that it is correct.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.