C 语言中的逻辑数组

3
我正在尝试测试一个由四个输入a、b、c和d组成的逻辑函数。
每个输入都是0或1。
我一直在尝试使用数组来完成这个任务。
如果logicXY数组中的列与combLogic数组匹配,我想返回1。
int comb(int** combLogic, int** logicXY){
    int x, y, logicOut;
    for ( x = 0; x < 4; x++ ){
        if (logicXY[0][x] == combLogic[x]){
                logicOut = 1;
                }
    }
    return logicOut;
}


int main void(){
    int** combLogic[4] = {a,b,c,d};  /*logic input*/
    int** logic[4][4] = {{1,0,0,1}, {1,0,1,1}, {0,1,0,0}, {0,1}}; /*return 1 if any of these combinations match the logic input*/
    int comb(combLogic, logicXY); /*send to function*/
}

我知道这个函数并不完整,但我认为我没有正确地传递数组。我已经阅读了许多教程,但似乎无法掌握理论。
编辑 我已经向前迈了几步,但它仍然不起作用。这是我现在拥有的。
.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;
}

主.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中的函数声明。
warning: parameter names (without types) in function declaration [enabled by default]

.h文件中的函数声明

int comb(logicInput,logicTest);

.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);

如果我偏离这段代码,似乎会导致编译器无法构建甚至更多的警告。


实际上问题出在combLogiclogic的声明上,你可以查看这段代码以了解如何声明/传递int二维矩阵?(链接:https://dev59.com/GHTYa4cB1Zd3GeqPs0rM#17567663) - Grijesh Chauhan
函数如何知道维度(特别是combLogic)? - huseyin tugrul buyukisik
1
问题出现在main()函数中数组的声明和参数列表中。请注意,int **paramint param[][4]不是同一类型。您的编译器应该会对数组中使用1作为初始化值而产生警告。它还应该会对调用comb()时的类型不匹配产生警告。 - Jonathan Leffler
2个回答

3
int * comblogic[4] = {a, b, c, d} //where a, b, c, and d, are arrays of size 4; aka int     a[4];
int logicArray[4][4] = values; 

在你的循环中:

int comb(int** combLogic, int** logicXY){
    int x, y, logicOut;
    for(int i = 0; i < 4; i++){
     for ( x = 0; x < 4; x++ ){
      if (logicXY[i][x] == combLogic[i][x]){
            logicOut = 1;
            break;
            }
}
   if(logicOut == 1)break; //incase you want to break when logicOut == 1 the first time it happens
}
return logicOut;
}

logicArrayvalues是什么?为什么要创建一个指向数组的指针数组呢?只是为了能够写int **logicXY吗?ams的答案更好。 - undur_gongor

2

这是错误的:

int comb(int** logicInput, int** logicTest)

试试这个:

int comb(int logicInput[4], int logicTest[4][4])

区别在于int **期望一个指向指针的指针(或指针数组),但是当你像定义数组一样定义时,根本没有任何指针,所以它不起作用。
技术上来说,当你将数组传递给函数时,它会取该数组的地址并通过引用传递。这意味着你有一个指向数组的指针,但是数组内部仍然没有指针。
当你传递一个数组时,必须始终说明除第一个轴之外所有轴的尺寸,否则编译器将不知道要跳过多少条目才能到达下一行。在这种情况下,我以int [4][4]的例子进行了说明,但它也可以是int [][4],如果你希望给出未知数量的数据行的话。
混淆的原因是C使用相同的a[x]符号来访问int **int[n][m],但它们在内存中看起来并不相同。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接