command line processing library - getopt

后端 未结 4 792
余生分开走
余生分开走 2021-01-05 15:42

Can someone help me with the getopt function?

When I do the following in main:

char *argv1[] = {\"testexec\",\"-?\"};
char *argv2[] = {\"testexec\",\         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-05 16:11

    argv1 and 2 must end in 0:

    char* argv1[] = {"par1", "par2", 0};
    

    Edit: OK, I read the getopt man page and I found this:

    The variable optind is the index of the next element to be processed in argv. The system initializes this value to 1. The caller can reset it to 1 to restart scanning of the same argv, or when scanning a new argument vector.

    So, making optind=1 between the two calls at getopt makes it work as expected.

提交回复
热议问题