簡體   English   中英

指針類型不兼容錯誤[C]

[英]Incompatible pointer type error [C]

我一直在告訴這一行代碼,從不兼容的指針類型傳遞參數。
這是代碼行:

if (linear_search (size_of_A, argv[i]))  

這是什么意思,我該如何解決? 這是整個程序:

int linear_search ( int size_of_A, char*argv[]){  
   int i;  
   i = 2;  
   while (i <= size_of_A - 1){  
      if (!strcmp (argv [1], argv[i])){  
      return 1;  
      }  
   }  
   return 0;  
}  

int main (int argc, char*argv []){  
   int size_of_A = argc - 2;  
   int i = 2;  
      if (linear_search (size_of_A, argv)){  
         printf ("%s not found\n", argv [1]);  
         return 1;  
      } else{  
         printf ("%s found\n", argv[1]);  
         return 0;  
      }  
      i = i + 1;  
   }  
}   

好的,這可以解決警告,但是現在當我通過編譯器運行程序時,什么也沒有發生。 應該告訴我第一個參數是否重復。
例如,輸出如下所示:

./a 3 hso 8 3  
3 found 

linear_search期望的數據類型為“ char ** ”。 您正在傳遞argv[i] ,它只是一個char* 嘗試像這樣傳遞“ argv”:

if (linear_search(size_of_A, argv))

您在linear_search存在邏輯錯誤。 您有一個基於i值的while循環,但是i從不改變。 您可能會缺少i++; 指令。

同樣(盡管這不會改變程序的行為): main的變量i從未真正使用過。 您可以刪除它,並且i = i + 1; 指令。

您正在將一個參數(第二個參數)傳遞給linear_search函數,該參數與預期的參數類型不匹配。 argv [i]具有char *類型,通常稱為字符串,而linear_search函數則期望char * [] ,它是字符串數組。

您的線性搜索功能僅將argv [1]與argv [2]進行比較,因為從未增加。 可能的解決方案(使用for循環而不是while,這更常見)是:

int linear_search ( int size_of_A, char*argv[]){  
   int i = 0;  // should always initialize in construction
   for ( i = 2; i < size_of_A; ++i ) {
      if (!strcmp (argv [1], argv[i])){  
        return 1;  
      }  
   }  
   return 0;  
}

變量i在main中從未真正使用過,您可以安全地刪除處理它的兩行。

您的函數需要一個char*[] (在這種情況下,應與char**等效)。 但是,通過

linear_search (size_of_A, argv[i])

您只是傳遞一個char*作為參數(因為[i]取消引用了一個指針)。 根據我在您的代碼中看到的內容,您可以嘗試使用

linear_search (size_of_A, argv+i)

但我不確定這是否會產生預期的行為。 理解C還為時過早:)

我不知道您的程序的邏輯,但解決了編譯器錯誤。 看到這個:

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

int size_of_A = argc - 2;  
int i = 2;  
if (linear_search (size_of_A, argv[])){  
     printf ("%s not found\n", argv [1]);  
     return 1;  
} 
int linear_search ( int size_of_A, char*argv[]){ 

char*argv[]意思是“指向char數組的指針”; 作為函數調用參數,與char**相同。

argv 一個char** ,但是argv[i]是一個char* ,因為C將argv[i]定義為*(argv + i )或者用英語“ dereference(argv plus i)”。 取消引用char**會使您留下char* ,而這並不是linear_search聲明要采用的。

如果您仍然感到困惑,請看這里。
`

當您向它傳遞“字符串”(argv [i])時,linear_search()接受“字符串數組”(指向字符數組的指針)。 我想做的是將linear_search稱為:

if (linear_search (size_of_A, (argv + i)))

盡管我不完全了解您的程序的邏輯...您是否要在后面的參數中搜索argv [1] ...?

這似乎是使用argv is NULL -terminated. You don't need to pass the count of items in it, but just do like this:的事實的好地方argv is NULL -terminated. You don't need to pass the count of items in it, but just do like this: argv is NULL -terminated. You don't need to pass the count of items in it, but just do like this:

 /* Searches through the NULL-aterminated array for the. * given needle string. Returns 1 if found, 0 if not. */ int linear_search (char **argv, const char *needle) { int i; for(i = 0; argv[i] != NULL; i++) { if(strcmp(argv[i], needle) == 0) return 1; } return 0; } 

Btw, I recommend comparing the return value of strcmp() nagainst literal 0, rather than using the ! notation, since the return value is in fact not boolean; it returns a int Btw, I recommend comparing the return value of strcmp() nagainst literal 0, rather than using the ! notation, since the return value is in fact not boolean; it returns a intBtw, I recommend comparing the return value of strcmp() nagainst literal 0, rather than using the ! notation, since the return value is in fact not boolean; it returns a int表示比較的元素之間的關系。

我意識到使用! 由於很多人認為C必須始終盡可能簡潔,因此這里的用法很常見,幾乎是慣用的,但是...我仍然建議不要這樣做。

oldfart:當心strcmp和所有無限的字符串函數(例如strcpy,但要小心strncpy:它不會添加尾隨NULL)。

youngunn:沒關系,因為argv有很多以NULL結尾的字符串!

作者:這種思維方式導致了緩沖區溢出的大量堆積。 argv已使用NULLS初始化。 但是從那以后它可能會改變。

y:但這是一個小程序,它永遠不會發生。

作者:當然。 程序不斷增長,“從未發生”的次數比您想象的要多。

y:好吧,偏執狂先生,我的strncmp限制應該使用多大的尺寸? 沒有明顯的數字可使用! 你是在建議我化妝嗎?

of:請參閱limits.h,尤其是ARG_MAX_POSIX_ARG_MAX以獲取可能的最大值。 如果需要,可以使用較小的值。 如果這樣做, 請將其記錄下來

y:所以我應該寫strncmp( arg, target, 5000 ) 好吧,先生偏執狂

作者:不要那樣做! #define APP_MAX_ARG 5000 ,然后使用strncmp( arg, target, APP_MAX_ARG ) 不要讓我開始使用魔術數字。 這些天孩子們。

作者:嘿,孩子, 滾下我的草坪

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM