2014-09-10 40 views
1

我得到這個錯誤:獲取有關的char *錯誤使用STRCMP(字符*,字符*)

$ gcc -Wall -g translate.c support.c scanner.c -o translate 
support.c: In function ‘translate’: 
support.c:148:13: warning: passing argument 1 of ‘strcmp’ from incompatible pointer type  [enabled by default] 
      compareNum = strcmp(dict[i], token); 
      ^
In file included from /usr/include/stdio.h:29:0, 
       from support.c:1: 
/usr/include/string.h:28:6: note: expected ‘const char *’ but argument is of type ‘char **’ 
int _EXFUN(strcmp,(const char *, const char *)); 
^

,這裏是功能轉換()

int 
translate(char* token, char** dict[], int dictLength) 
{ 
    int i = 0; 
    int compareNum; 

    for(i=0;i<dictLength;i++) 
    { 
     if(i % 2 == 0) 
     { 
      compareNum = strcmp(dict[i], token); 
      ++i; 
      if(compareNum == 1) 
      { 
       return i; 
      } 
     } 
    } 

return 0; 

} 

一些原因我正在傳遞字典[我],這是一個字符串數組,我試圖比較數組的每個偶數元素的字符串標記,但它說它的char **。我知道該數組是char **,但不是該元素是char *?

+7

字符** []是字符數組* *,不是char *數組。 – IdeaHat 2014-09-10 18:24:12

+0

顯示你的字典的聲明 – Pradheep 2014-09-10 18:25:06

+0

@Pradheep他做了,它的函數的參數 – IdeaHat 2014-09-10 18:25:27

回答

3

dict參數聲明,像這樣:

char** dict[] 

所以dict[i]char**類型。因此錯誤。

我想爲了讓我們提供進一步的建議,我們需要在調用此函數時提供作爲dict參數提供的對象的一些詳細信息。也許你只需要改變參數的聲明是:

,我會強烈建議
char* dict[] 

有一件事是在聲明這些參數時使用const,允許你通過不可修改的字符串。

1

dict參數聲明:

char** dict[] 

這是char**數組。

這意味着dict[index]char**

改變參數

char** dict[] 

char* dict[] 

應該解決您的問題