"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Mark" <so***@localbar.com> writes: "Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org... Kenneth Brody <ke******@spamcop.net> writes:
Cong Wang wrote:
[...]
> PS: Changing the arguments of a function isn't a good habit.
I guess you've never used sprintf, strcpy, fread, ...
Those functions don't (necessarily) change their parameters; they
change the objects that their parameters point to.
Which is exactly what Cong complained about.
What was being called a bad habit was this kind of thing:
void foo(int x) {
{
x ++; /* changing the parameter */
}
No, it wasn't that kind of thing.
Yes, it was; see below.
There's nothing incorrect about doing this, but it can cause confusion
I disagree. Personal style issue, nothing more.
(though of course it won't affect the actual argument that was passed
to the function). Whether it's good or bad style in general is a
debate that I'm not going to get into, at least for now.
Good... because the actual code in question was as follows:
void reverse(char *s)
{
char t;
char* t_str;
for (t_str = s; *t_str != '\0'; t_str++);
t_str--;
for (; s < t_str; s++, t_str--) {
t = *s;
*s = *t_str;
*t_str = t;
}
}
Sometimes it is the PURPOSE of a function to change the object
that the parameters point to... Ken was merely trying to point that out.
I think you missed it. The argument to reverse() is called s. The
function changes the value of s in the second for loop; see "s++".
(It also changes what s points to, but that's a different matter.)
Yes, I missed it :-)
Thanks for pointing that out.
After re-reading the thread I see that rather than sprintf() strcpy()
and fread() Ken should have said strcat(), strcpy(), strcmp() !!!
Many implementations of those functions do 'change' their parameters,
and why not? Parameters belong to the function... they are theirs
to use and change as the function sees fit.
Now please allow me to take this thread in another direction...
(I think I already did ;-) I know you don't want to get into the
'style' issue, but why not?
To make a broad statement such as: changing the arguments of a
function isn't a good habit - is not a good habit. Depending on the
circumstances, it doesn't bother me in the least to see it done.
consider the following:
void
do_something(int max_times)
{
while(max_times--)
something();
return;
}
Looks fine to me... doesn't bother me in the least.
Personally, I prefer that to code which used a copy
of max_times for no other reason than to be anal.
I've seen the arguments before...
for: clean concise code.
against: can't determine initial value.
for: who gives a 5hit? I don't need the initial value.
against: something() may break, debugger lost the initial value.
for: go up a frame
.... [it goes on and on] ...
against: I once read on comp.lang.c that it's not a good habit.
for: *&$K them too :-)
Which side of the fence are you guys on? :-)
For those on the 'against' side: you may want to take a look
at your implementation's standard library functions, you may
have to rewrite some of them if it bothers you that much.
I look forward to reading all responses... on Monday!
Have a nice weekend guys :-)
Mark