Why check (* argv == NULL)?

In the data structures class that I am currently accepting, we were tasked with writing a C ++ web crawler. To give us a start, the professor provided us with a program to get the source from a given URL and a simple HTML parser to remove tags. The main function for this program takes arguments and therefore uses argc / argv. The code used to verify the arguments is as follows:

// Process the arguments if (!strcmp(option, "-h")) { // do stuff... } else if (!strcmp(option, "")) { // do stuff... } else if (!strcmp(option, "-t")) { // do stuff... } else if (!strcmp(option, "-a")) { // do stuff... } if ( *argv == NULL ) { exit(1); } 

If the option option was populated with a switch in argv [1], and argv [2] and above have the remaining arguments. The first block, which I understand, is very good, if the switch is equal to the line, it all depends on the switch. I am wondering what the purpose of the last if block is.

My C ++ may be a little rusty, but I seem to recall that argv is equivalent to argv [0], which basically means that it checks for arguments. In addition, I had the impression that argv [0] always (at least in most implementations) contained the name of the program to run. It seems to me that argv [0] can be null if argc is 0, but when I searched on Google, I could not find a single message determining whether this was possible.

And so I am turning to you. What exactly is final if block checking?

EDIT:. I came to the reasoning given in the comments on the selected answer that it may be possible to deliberately cause argv [0] to become NULL or otherwise to become NULL based on a platform-specific implementation of the core.

+11
c ++ c null main argv
Feb 25 '10 at 17:16
source share
4 answers

argc will give you the number of command line arguments passed. You do not need to check the contents of argv too if there are not enough arguments.

 if (argc <= 1) { // The first arg will be the executable name // print usage } 
+9
Feb 25 '10 at 17:18
source share
β€” -

3.6.1 / 2:

If argc is nonzero , these arguments must be provided in argv [0], although ... and argv [0] must be a pointer to the initial NTMBS character which is the name used to invoke the program or "". The argc value must be non-negative. The value of argv[argc] must be 0.

The emphasis is mine. argc is only guaranteed non-negative, not non-zero.

This is at the entrance to the main. It is also possible that //do stuff changes the argv value or the contents of the array it points to. This is not completely unheard of for option processing code to bias values ​​with argv as they are processed. Therefore, the *argv == null test can be checked to see if any command line arguments remain after the parameters have been deleted or skipped. You will have to look at the rest of the code.

+10
Feb 25 '10 at 17:26
source share

Remembering how portable C is, it may not always work on a standard platform such as Windows or Unix. Perhaps this is some kind of microcode inside your washing machine that runs on some kind of cheap, hacked environment. Thus, it is good practice to make sure that the pointer is not null before casting it, which could lead to this issue.

However, you are right. * argv matches argv [0], and argv is supposed to be initialized by the environment, if one is provided.

+4
Feb 25 '10 at 17:25
source share

just speculation.

What if your professor refers to this?

 while(*++argv !=NULL) printf("%s\n",*argv); 
+4
Feb 25 '10 at 17:33
source share



All Articles