[ACCEPTED]-Why check if (*argv == NULL)?-argv

Accepted answer
Score: 12

3.6.1/2:

If argc is non-zero those arguments shall be provided 17 in argv[0] though ... and argv[0] shall 16 be the pointer to the initial character 15 of a NTMBS that represents the name used 14 to invoke the program or "". The value 13 of argc shall be nonnegative. The value of 12 argv[argc] shall be 0.

Emphasis mine. argc is only 11 guaranteed non-negative, not non-zero.

This 10 is at entry to main. It's also possible 9 that //do stuff modifies the value of argv, or the contents 8 of the array it points to. It's not entirely 7 unheard of for option-handling code to shift 6 values off argv as it processes them. The 5 test for *argv == null may therefore be testing whether 4 or not there are any command-line arguments 3 left, after the options have been removed 2 or skipped over. You'd have to look at the 1 rest of the code.

Score: 10

argc will provide you with the number of command 3 line arguments passed. You shouldn't need 2 to check the contents of argv too see if there 1 are not enough arguments.

if (argc <= 1) { // The first arg will be the executable name
   // print usage
}
Score: 4

Remembering just how portable C is, it might 10 not always be running on a standard platform 9 like Windows or Unix. Perhaps it's some 8 micro-code inside your washing machine running 7 on some cheap, hacked environment. As such, it's 6 good practice to make sure a pointer isn't 5 null before dereferencing it, which might 4 have led to the question.

Even so, you're 3 correct. *argv is the same as argv[0], and 2 argv is supposed to be initialized by the environment, if 1 it's provided.

Score: 4

just a speculation.

what if your professor 1 is referring to this ??

while(*++argv !=NULL)

    printf("%s\n",*argv);

More Related questions