在我大學一年級的課上,我們剛剛開始擺弄陣列,在作業表上我得到了這段代碼,這似乎不起作用。我一直在掃描并尋找問題,但似乎沒有任何解決方法。這是我的代碼...
#include <stdio.h>
#include <stdbool.h>
int main(){
int size = 10;
float suspect[size]; //Declaring suspect array
int sizeR = 3;
int sizeC = 10;
float criminals[sizeR][sizeC]; //Declaring criminals array
//Read 10 input values into suspect array from keyboard
printf("Enter the 10 chromosomes of the suspect separated by spaces: \n");
for (int i = 0; i < size; i )
scanf(" %f", &suspect[i]);
//Read multiple profiles of 10 values into criminals array from the keyboard
for (int i = 0; i < sizeR; i ){
printf("Enter the 10 chromosomes of the %dth criminal: \n", i 1);
//Read 10 input values of a criminal into criminals array from the keyboard
for (int j = 0; j < sizeC; j )
scanf(" %f", &criminals[i][j]);
}
//Match two profiles
bool match = true;
for (int i = 0; i < size; i )
if(suspect[i] != criminals[i]) //Error is in this line
match = false;
//Display matching result
if (match)
printf("The two profiles match! \n");
else
printf("The two profiles don't match! \n");
return 0;
}
當我運行此代碼時,我回傳:
錯誤:二進制運算式的無效運算元('float' 和 'float [sizeC]')
錯誤指向匹配的兩個組態檔部分中的 != 。對不起,如果解決方案很簡單,編碼對我來說相對較新,我正在努力使用谷歌找到這個特定問題的解決方案。
uj5u.com熱心網友回復:
在這個 if 陳述句中
if(suspect[i] != criminals[i]) //Error is in this line
運算式criminals[i]被隱式轉換為型別,float *因為轉換前運算式的原始型別是float[sizeC]。
此外,運算式suspect[i]具有型別float。也就是說,將型別的物件float與float *沒有意義的型別的指標進行比較。
所以編譯器會發出錯誤資訊。
如果要將陣列suspect與二維陣列的元素進行比較,criminals則應再使用一個內部 for 回圈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/353291.html
上一篇:【力扣】56. 合并區間
下一篇:2021-11-08每日刷題打卡
