C警告从不兼容的指针类型传递'getopt'的参数2

时间:2013-10-07 14:32:46

标签: c

我用getopt()编写一个函数来从命令行获取选项。当我编译它时,我得到这个警告:

cc1: warnings being treated as errors
csim.c: In function ‘getArg’:
csim.c:157: error: passing argument 2 of ‘getopt’ from incompatible 
pointer type /usr/include/getopt.h:152: note: expected ‘char * const*’ 
but argument is of type ‘const char **’

这是C代码:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
}

int getArg(int argc, char const *argv[], int *verbose, int *ps, 
    int *pE, int *pb, char *traceFileName){
    int arg;
    int argCount;
    while ((arg = getopt(argc, argv, "vs:E:b:t:")) != -1){
        switch (arg){
            case 'v':
            *verbose = 1;
            break;

            default:
            printf("%s\n", "Illegal command arguments, please input again");
            exit(-1);
            break;
        }
    }

    if(argCount < 4){
        printf("%s\n", "Illegal command arguments, please input again");
            exit(-1);
    }
    return 0;
}

2 个答案:

答案 0 :(得分:2)

问题是,正如错误所说,您传递const char **,其中char * const*是预期的。具体来说,您将argv(其类型错误)传递给getopt。您可以通过更改argv的类型来解决此问题。

int getArg(int argc, char * const argv[], int *verbose, int *ps, int *pE, int *pb, char *traceFileName)

答案 1 :(得分:0)

这是你的函数声明argv的方式。你改变了常量。错误消息告诉您出了什么问题。