Re: Processing command line arguments



On Apr 21, 10:52 am, "Malcolm McLean" <regniz...@xxxxxxxxxxxxxx>
wrote:
"Gregor H." <nomail@invalid> wrote in message

(Why did you start a new thread to reply to this question?)

news:maij2396rr5qli9a1lndf3cv5g4g5mgjpr@xxxxxxxxxx

In K&R (2nd edition) the authors published the following code for
dealing with command line arguments (p. 117):

while (--argc > 0 && (*++argv)[0] == '-')
while (c = *++argv[0])
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}

But this solution raised some criticism:

"The sample program incrementsargv[0]. Althoughargvis
modifiable, andargv[0][0] is modifiable (ifargv[0] is
not a null pointer),argv[0] is not modifiable. (At least,
not always.)"

(Peter Seebacher,http://www.plethora.net/~seebs/c/knr.html)

"p.117 (§5.10): In the find example, the program increments
argv[0]. This is not specifically forbidden, but not
specifically allowed either."

(Source:
Errata for The C Programming Language, Second Edition
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html)

Now my question is: would the following (slight modification of the
original code) be a reasonable (or at least correct) solution?:

while (--argc > 0 && (*++argv)[0] == '-') {

This line is virtually unreadable. I know K and R used the same construct.
Things have moved on.

Say what? It's terse, clear, and correct - what more can you ask for
in a line of code?

char *arg = *argv;
while (c = *++arg)

However this is the way round.argvcontains a list of strings. Nothing
conceptually difficult about that, but you can make it look difficult
because strigns are not atomic objects in C. It is a bad idea to modify the
strings in place, though I think they are writeable for historical reasons,
and it is a bad idea and may even be illegal to modify the pointers in theargvarray.
However if you take copies then you can do what you want with the copy,
naturally.
--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm


.



Relevant Pages

  • Re: The Coalition against Personal Firewalls
    ... correct changes. ... You can modify the code as YOU like. ... It seems you don't know that modern compilers do a lot of optimization ... or strings. ...
    (comp.security.firewalls)
  • Re: Where can I change international system popup message contents?
    ... one can just modify the variable of IDS_CONFIRM_DEL_RECBIN from ... But it is only applicable to english os. ... like you would find the LOC_* strings in common.str. ... IDS_CONFIRM_DEL_RECBIN is local to the ceshui component. ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Useless obfucation in synopsis for fopen
    ... This expresses very clearly that fopen is not allowed to modify ... strings, behaviour is undefined. ... When fopen figures out that there is no file named "nonexisting file" ... and tries to set errno, the result is undefined behaviour and your ...
    (comp.std.c)
  • Re: List mapping question
    ... Marc Huffnagle wrote: ... > I have a number of variables that I want to modify (a bunch of strings ... > that I need to convert into ints). ...
    (comp.lang.python)
  • Re: Adjusting file times
    ... Bob Butler wrote: ... They are correct when used to create the strings. ... I see 2 odd things in the original code you posted... ...
    (microsoft.public.vb.winapi)