Newsgroups: comp.std.c
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!barmar
From: barmar@think.com (Barry Margolin)
Subject: Re: A small programming challenge.
Message-ID: <1991May16.192050.6430@Think.COM>
Keywords: Algorithm
Sender: news@Think.COM
Reply-To: barmar@think.com
Organization: Thinking Machines Corporation, Cambridge MA, USA
References: <1499@loki.une.oz.au> <1991May16.073245.12652@nodecg.ncc.telecomwa.oz.au>
Date: Thu, 16 May 91 19:20:50 GMT

In article <1991May16.073245.12652@nodecg.ncc.telecomwa.oz.au> kevin@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@think.com
{uunet,harvard}!think!barmar
