How big is the argv[] array?

hood at osiris.cso.uiuc.edu hood at osiris.cso.uiuc.edu
Tue Oct 4 03:21:00 AEST 1988


How big is the argv[] array?  Or to ask it another way, how safe is it to
go past the argc-1'th element in the argv[] array?

Isn't it true that the array of pointers (or pointer to pointers, depending
on your point of view) "argv" actually contains argc+1 elements, and not
argc elements?

Consider the following command line, "command this is a test".  Is argv
passed to the program like this:

	argc = 5				argc = 5
	argv[0] = command			argv[0] = command	
	argv[1] = this				argv[1] = this
	argv[2] = is		or is it	argv[2] = is
	argv[3] = a				argv[3] = a
	argv[4] = test				argv[4] = test
	argv[5] = NULL

I had read somewhere that argc was not really required to process command
line arguments because, you could test for the NULL in the array to
signal the end of the arguments.

Consider this code fragment where I "walk off the end" of argv[] if argc
is equal to 1.  Is this guaranteed to work?

main(argc, argv)
int argc;
char *argv[];
{
	int i;
					/* fake an argument */
	if (argc == 1) {
		argv[argc] = "dummy";
		argc++;
	}
					/* process arguments */
	for (i=1; i<argc; i++) {
		.
		.
	}
}

Emmet P. Gray				US Army, HQ III Corps & Fort Hood
...!uunet!uiucuxc!fthood!egray		Attn: AFZF-DE-ENV
					Directorate of Engineering & Housing
					Environmental Management Office
					Fort Hood, TX 76544-5057



More information about the Comp.lang.c mailing list