1
我編寫了一個函數getopt()
以從命令行獲取選項。當我編譯它,我得到這樣的警告:C警告從不兼容的指針類型傳遞'getopt'的參數2
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;
}
thanks davmac!我看到在一些代碼中main()函數就像main(int argc,char ** argv)。那麼,char * const argv [],char const * argv []和char ** argv有什麼區別? – violethaze
'char * const argv []'是一個不可修改的'char *'值的數組。它可以轉換爲一個指針(特別是'char * const *',即指向不可修改的'char *'值的指針)。 'char const argv []'將是一個不可修改的char值數組。 'char ** argv'是一個指針指向字符的指針;它可以被看作是一個'char *'的數組。 – davmac