2

我编写了一个函数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;
}
4

2 回答 2

3

问题是,正如错误所说,您正在传递 a预期的const char **地方。char * const*具体来说,您将argv(类型错误)传递给 getopt。您可以通过更改argv.

int getArg(int argc, char * const argv[], int *verbose, int *ps, int *pE, int *pb, char *traceFileName)
于 2013-10-07T14:44:01.547 回答
0

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

于 2013-10-07T14:43:36.840 回答