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.
c ++ c null main argv
Shaun Hamman Feb 25 '10 at 17:16 2010-02-25 17:16
source share