3

I am trying to test a logic function from four inputs a,b,c and d.

Each input is either 0 or 1.

I have been trying to accomplish this with arrays.

I want to pass back a 1 if a column in logicXY array matches that of the combLogic array.

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*/
}

I know the function is not complete but I don't think I am passing the arrays correctly. I have read a number of tutorials but I cant seem to grasp the theory.

EDIT I have got a few steps forward but it still isn't working. This is what I have now.

Function declaration in .h

int comb(logicInput,logicTest);

Function in .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;
}

The loop in part of the 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;

The code steps over the int comb(logicInput,LogicTest) and never carries out the function. If I take out int from the line then it carries out the function, returns the value but when the value is written to output it is nothing like the value that was returned from the function.

EDIT

I have made a few changes to the code so it does appear to work and with only one warning from the compiler for the function declaration in .h that I cannot seem to fix.

warning: parameter names (without types) in function declaration [enabled by default]

Function declaration in .h

int comb(logicInput,logicTest);

Function in .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;
}

Loop in 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);

If I deviate from this code I just seem to end up with the compiler failing to build and even more warnings.

  • actually problem is with declarations of `combLogic` and `logic`, you can check this code for [How to declare/pass `int` 2D-martix ?](http://stackoverflow.com/questions/17566661/c-dynamic-array-initalization-with-declaration/17567663#17567663) – Grijesh Chauhan Jul 11 '13 at 19:53
  • How does the function know the dimensions (especially combLogic)? – huseyin tugrul buyukisik Jul 11 '13 at 19:54
  • 1
    The problems are in the declarations of the arrays in `main()`, and also in the parameter list. Note that `int **param` and `int param[][4]` are not the same type. Your compiler should be complaining about the use of `1` as an initializer in the arrays. It should also be complaining about type mismatches in the calls to `comb()`. – Jonathan Leffler Jul 11 '13 at 20:06

2 Answers2

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; 

in your loop:

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;
}
KrisSodroski
  • 2,796
  • 3
  • 24
  • 39
  • What is `logicArray` and `values`? And why create an array of pointers to arrays at all? Just to be able to write `int **logicXY`? ams' answer is much better. – undur_gongor Jul 12 '13 at 13:54
2

This is wrong:

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

Try this:

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

The difference is that int ** expects a pointer to a pointer (or an array of pointers), but when you define an array like you have then there are no pointers at all, so it doesn't work.

OK, technically, when you pass an array to a function it takes the address of that array and passes it by reference. This means you have a pointer to an array, but there are still no pointers inside that array.

When you pass an array you must always say what the dimensions of all but the first axes are, or else the compiler will not know how many entries to skip to get to the next row. In this case I gave the example as int [4][4], but it could just as well have been int [][4], should you have wished to give an unknown number of data rows.

The confusion arrises becuase C uses the same a[x] notation to access both int ** and int[n][m], but they do not look the same in memory.

ams
  • 24,923
  • 4
  • 54
  • 75