Re: *z++ and strings
From: Bernhard Holzmayer (holzmayer.bernhard_at_deadspam.com)
Date: 03/19/04
- Previous message: John L: "Re: Code to connect to MSSQL Database"
- In reply to: RoSsIaCrIiLoIA: "*z++ and strings"
- Next in thread: RoSsIaCrIiLoIA: "Re: *z++ and strings"
- Reply: RoSsIaCrIiLoIA: "Re: *z++ and strings"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 19 Mar 2004 08:24:23 +0100
RoSsIaCrIiLoIA wrote:
> while(*z++=*v++);
> --z; v=" ;\n";
> while(*z++=*v++);
I don't want to echo Arthur's comments, let me add this note:
don't create such "compact" code.
This was (at least might have been) of such advantage in times when
compilers did a bad job in optimization (loop unfolding).
Nowadays, you'll usually get code of same (or even better)
performance, if you just write what you want, step by step.
Your code fragment would work the same if you changed it to the
following:
while(*v) {
*z++=*v++;
}
v=" ;\n";
while (*v) {
*z++=*v++;
}
*z='\0';
The essential change is that the condition after while is separated
from the assignment.
It's the same with an if () construct. Don't do assignment inside
the conditional term's bracket.
If you have to localize errors, they will often be found in such
constructs.
Besides: did you notice, that you could have written your code much
simpler, using library commands such like printf, something like:
u=rand();
if (u & 1) {
printf("YES;\n");
} else {
printf("NO;\n");
}
much more readable, thus much less possibilities to make errors.
Or, if this doesn't make sense in your real program, check, if you
can use strcpy and/or strcat.
If you watch out for coding dangers, you'll certainly find out that
you make lots of errors if you try to reimplement library code,
which you could avoid if you just would use these functions.
Last hint: get the 'lint' tool. Depending on your OS, you'll find
'pclint', 'lclint', ...
This tool checks your code, and depending on its configuration,
would have provided most of the hints which you got from Arthur or
from my side.
It's good practice, to run all of your code through lint before
releasing a piece of software.
Bernhard
- Previous message: John L: "Re: Code to connect to MSSQL Database"
- In reply to: RoSsIaCrIiLoIA: "*z++ and strings"
- Next in thread: RoSsIaCrIiLoIA: "Re: *z++ and strings"
- Reply: RoSsIaCrIiLoIA: "Re: *z++ and strings"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|