Difference between (*++argv)[0] and while(c = *++argv[0])

后端 未结 5 2047
后悔当初
后悔当初 2020-12-09 18:33

I have the following snippet of code:

int main(int argc, char *argv[])
{   

     char line[MAXLINE];
     long lineno = 0;
     int c, except = 0, number =          


        
5条回答
  •  甜味超标
    2020-12-09 19:12

    yes, you are correct.

    while(--argc > 0 && (*++argv)[0] == '-')
    

    is scanning the array (of length argc) of command line arguments one by one looking for those starting with a - option prefix. For each of those:

    while(c = *++argv[0])
    

    is scanning through the set of switch characters that follow the first - in the current argument (i.e. t and n in -tn, until it hits the string null terminator \0, which terminates the while loop, since it evaluates as false.

    This design allows both

    myApp -t -n
    

    and

    myApp -tn
    

    to both work and be understood as having the options t and n.

提交回复
热议问题