A small programming challenge.
Barry Margolin
barmar at think.com
Fri May 17 05:20:50 AEST 1991
In article <1991May16.073245.12652 at nodecg.ncc.telecomwa.oz.au> kevin at nodecg.UUCP (Kevin Spencer) writes:
> for (i=1; i<4; i++)
> args[i] = (i<argc ? argv[i] : argv[argc-1]);
This never sets args[4] (and in fact, the declaration I didn't include in
the above quote doesn't declare it large enough). Also, it doesn't default
args[1] properly when no arguments are given. It ends up being defaulted
to argv[0] (usually the program name) rather than the intended hardcoded
default.
The code should be something like
void main (argc, argv)
int argc;
char *argv[];
{
char *args[4];
args[0] = (argc < 2) ? DEFAULT : argv[1];
for (i = 1; i < 4; i++) {
args[i] = (i+1 < argc) ? argv[i+1] : args[i-1];
}
...
}
--
Barry Margolin, Thinking Machines Corp.
barmar at think.com
{uunet,harvard}!think!barmar
More information about the Comp.std.c
mailing list