argv

A

aegis

if argc is not zero then argv[0] is guaranteed
to point to some string. In which case,
if argc is not zero then argv[0] will
never be NULL. Valid deduction from the standard?
 
M

Mike Wahler

aegis said:
if argc is not zero then argv[0] is guaranteed
to point to some string.

Yes, which means that argv[0] is not a null pointer.
(a null pointer by definition cannot point to a string,
or any other object).
In which case,
if argc is not zero then argv[0] will
never be NULL. Valid deduction from the standard?

:)

Here's the quote from the standard, for completeness:

========================================================================
ISO/IEC 9899:1999 (E)

5.1.2.2.1 Program startup

2 If they are declared, the parameters to the main function
shall obey the following constraints:

-- The value of argc shall be nonnegative.

-- argv[argc] shall be a null pointer.

-- If the value of argc is greater than zero, the array members
argv[0] through argv[argc-1] inclusive shall contain pointers
to strings, which are given implementation-defined values by
the host environment prior to program startup. The intent is
to supply to the program information determined prior to program
startup from elsewhere in the hosted environment. If the host
environment is not capable of supplying strings with letters in
both uppercase and lowercase, the implementation shall ensure
that the strings are received in lowercase.

-- If the value of argc is greater than zero, the string pointed to
by argv[0] represents the program name; argv[0][0] shall be the
null character if the program name is not available from the host
environment. If the value of argc is greater than one, the strings
pointed to by argv[1] through argv[argc-1] represent the program
parameters.


-- The parameters argc and argv and the strings pointed to by the
argv array shall be modifiable by the program, and retain their
last-stored values between program startup and program termination.
========================================================================


-Mike
 
L

Lawrence Kirby

if argc is not zero then argv[0] is guaranteed
to point to some string. In which case,
if argc is not zero then argv[0] will
never be NULL. Valid deduction from the standard?

Yes, and in that case argv[0] will point to a zero length string if the
program name is not available.

Lawrence
 
D

Dan Pop

In said:
if argc is not zero then argv[0] is guaranteed
to point to some string. In which case,
if argc is not zero then argv[0] will
never be NULL. Valid deduction from the standard?

Yup. As a practical consequence, you can traverse the whole program
parameter list without using argc at all:

#include <stdio.h>

int main(int argc, char **argv)
{
while (*argv != NULL) puts(*argv++);
return 0;
}

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top