B
Beej Jorgensen
bartc said:How about here:
int main(int argc, char **argv) ?
Nothing is fundamentally wrong with modifying those, or the strings
pointed to by argv:
C99 5.1.2.2.1p2:
# The parameters argc and argv and the strings pointed to by the argv
# array shall be modifiable by the program, and retain their last-stored
# values between program startup and program termination.
I've seen argc modified in command line parsing, and it's not entirely
rare in Unix to modify argv which can, on some(?) Unices, cause the "ps"
command to show different values for the names of running programs.
Personally, I leave argc and argv as-is--but I acknowledge there is no
reason for that other than preference.
Parameters are always input data to a C function. You can modify them if
they are not quite right, or if they are doing double duty as local
variables. But usually they are left alone.
One argument for leaving them alone might be one of code maintenance.
For instance, if one is modding a large function and sees parameter
numCPUs, one is probably going to expect it contains the number of CPUs.
But this assumption will be wrong at the end of the function if there is
something like a while(numCPUs--) loop in there somewhere. (A point
which would be caught with a code read or test, but then you're just
going to need to store numCPUs in another temp variable, anyway...)
As inputs to the function, I feel there is something slightly more
sacred to the parameters than typical local variables, I guess! If
sqrt() gets parameter x, its entire external world is x; everything it
needs to know about everything needed for its calculation is x*. So I
suppose I'd prefer to leave its modification until the end of the
function when its no longer needed (e.g. in strcpy(), feel free to
modify the "src" parameter because it happens at the end of the
function.)
-Beej
* exceptions aside