TheValars
TheValars

Reputation: 281

What is this if statement checking?

This is a function I obtained from class. I am just confused with the flow control here. The first if statement checks if there is the function and at least one argument. The second else if flow control, to my understanding, is checking if argv[1] is NOT a NULL pointer. My question is, if the first flow control is passed, would that mean the second flow control will definitely pass? How do we reach the third flow control in this case? Is my understanding of the second flow control incorrect?

int my_function(int argc,char **argv)
{
    if (argc < 2) {
        fprintf(stderr,"not enough arguments" );
        return(1);
    }
    else if (argv[1]) 
        return(atoi(argv[1])); 
    else 
        exit(0);

}

~

Upvotes: 0

Views: 115

Answers (4)

CinCout
CinCout

Reputation: 9619

This is a if-else if-else ladder. Only one of this will run at a time.

So, if you send less than 2 arguments (if case), "not enough arguments" will be printed and my_function() will return 1, which denoted an error condition.

If you send more than 2 arguments (argc >= 2), and argv[1] isn't NULL (else-if case), my_function() will return atoi(argv[1]).

Finally, if both the above conditions are false, i.e., if argc >= 2 && NULL == argv[1], my_function() will return 0. Now this is incorrect in my opinion, since returning 0 means success, which is clearly not the case here.

EDIT: Now the code exits in the else case, which seems to be OK.

Upvotes: 3

librik
librik

Reputation: 3788

You're absolutely right, assuming the "usual" semantics of argc and argv. Under the standard interpretation of argv and argc in a main() function, the third clause is very unlikely to ever be reached.

If argc >= 2, then argv[0] and argv[1] point to null-terminated char* strings, and argv[2] is a NULL pointer. The only way argv[1] would be a NULL pointer is if argc == 1 ... which is already caught by the first if condition!

I suppose this could be seen as very safe coding, but if things are that badly screwed up I would just let it crash.

Upvotes: 2

Ray
Ray

Reputation: 1985

You would be correct if that function were main. The only way that argv[1] wouldn't be true would be if argc < 2. So you could never reach the third branch.

But since it's a separate function, you could do the following, and the third branch would trigger.

int main(void) {
    char *argv[] = { NULL, NULL };
    int r = my_function(42, argv); // r == 0
}

(Of course, no sane coder would give a function argc and argv parameters without also making them have the same semantics as main's.)

Upvotes: 2

cbranch
cbranch

Reputation: 4769

Your understanding is correct -- the else if statement is checking to see if argv[1] is not NULL.

If argc and argv are the same parameters that are passed into main() by the runtime library, then argv[1] will never be NULL when argc >= 2, so the final else is unreachable code. However, if this function is called with parameters that are obtained elsewhere, then there is no such guarantee.

Upvotes: 4

Related Questions