2017-04-24 35 views
-2

我在使用這段代碼的麻煩:串信相比,不兼容的參數

char myword[] = "ABC\0"; 
    // for loop for length of array 
    for (int i = 0; i<strlen(myword); i++) { 
    if (strcmp(myword[i], 'A') == 0) {... 

應該檢查是否是我的信件等於A.是否嘗試過進入ABC不同字母但錯誤:

Error: Argument of type "char" is incompatible with parameter of type "const char *" in "main.cpp"

不斷出現。

+2

C不是C++ - 不要垃圾標籤。請發佈[MCVE]。 –

+0

您的字符串長度爲5個字符,並帶有兩個'\ 0'字符。我猜這不是你想到的。 – chris

+0

您正在使用strcmp來比較單個字符。 strcmp用於比較字符的數組或字符串 –

回答

2

strcmp()函數比較字符串,而不是個別字符。您可以比較使用內置==操作字符:

char myword[] = "ABC"; // (1) 
// for loop for length of array 
for (int i = 0; i<strlen(myword); i++) { 
    if (myword[i] == 'A') { ... } 
} 

順便說一句,有沒有需要手動添加就行空終止(1) - 語言將增加這個給你。

-1
if (strcmp(myword[i], 'A') == 0) { 

strcmp是有比較字符串,而不是單個字符。只是嘗試直接比較字符,即

if (myword[i] == 'A') {