我想從四個輸入a,b,c和d測試一個邏輯函數。在C中的邏輯陣列
每個輸入是0或1.
我一直在嘗試用數組來完成這個任務。
我想回傳一個1,如果logicXY數組中的一列與combLogic數組的列相匹配。我知道函數不完整,但我不認爲我正確地傳遞數組。我讀了一些教程,但我似乎無法理解這個理論。
編輯 我已經有了幾步,但它仍然無法正常工作。這是我現在擁有的。在.H
int comb(logicInput,logicTest);
功能
函數聲明以.c
/* Function - Combination */
int comb(int** logicInput, int** logicTest){
int x, y, logicOut;
for (x = 0; x < 4; x++){
if (logicTest[0][x] == logicInput[x]){
logicOut = 1;
}
}
return logicOut;
}
環路在main.c中的一部分
int output = 0;
int logicInput[4] = {0,1,1,1};
int logicTest[4][4] = {{1,0,0,1}, {1,0,1,1}, {0,1,0,0}, {0,1,1,1}};
int comb(logicInput,logicTest);
output = comb;
在int comb(logicInput,LogicTest)
的代碼步驟和從不執行功能。 如果我從行中取出int
然後它執行該函數,返回該值,但是當該值寫入輸出時,它與從該函數返回的值不同。
編輯
所以它似乎工作,從編譯器中的.h函數聲明,我似乎無法修復只有一個警告,我已經對代碼進行一些更改。在.H
int comb(logicInput,logicTest);
功能
warning: parameter names (without types) in function declaration [enabled by default]
函數聲明以.c
int comb(int** logicInput, int** logicTest){ /*Points to the arrarys in the main program*/
int x, i, logicOut;
for(i = 0; i < 4; i++){ /*test each column*/
for (x = 0; x < 4; x++){ /*test each row*/
if (logicTest[i][x] == logicInput[i][x]){
logicOut = 1;
break;
}
}
if(logicOut == 1)break; /*Break when logicOut == 1 the first time it happens*/
}
return logicOut;
}
循環main.c中
int output;
int logicInputC1[4] = {0,1,0,1};
int logicTestC1[4][4] = {{1,0,0,1}, {1,0,1,1}, {0,1,0,0}, {0,1,0,1}};
output = comb(logicInputC1,logicTestC1);
如果我從這個代碼偏離我只是似乎最終導致編譯器無法構建甚至更多的警告。
實際上問題是'combLogic'和'logic'的聲明,你可以檢查這個代碼[如何聲明/傳遞'int' 2D-martix?](http://stackoverflow.com/questions/17566661/ c-dynamic-array-initalization-with-declaration/17567663#17567663) –
函數如何知道尺寸(尤其是combLogic)? –
問題出在'main()'中的數組聲明中,也出現在參數列表中。請注意,'int ** param'和'int param [] [4]'是不同的類型。您的編譯器應該抱怨在數組中使用'1'作爲初始值設定項。它也應該抱怨調用'comb()'的類型不匹配。 –