Search code examples
cfunctionmatrixintuint8t

Matrix of type int using uint8_t data prints wrong when passed to a function


Ok so I got the following pointer sh and the array of matrices shadows.

 uint8_t * sh;
 int shadows[k][330][210];

I fill the shadows like this:

int rows = 165;
int cols - 105;
for(int i = 0; i < rows; i++){
        for(int j = 0; j < cols; j++){              
            shadows[reached][i][j] = sh[index];
            index++;
        }
}

Then I can print for example shadows[0] like this:

int i;
int j;
for (i=0; i<rows; i++){
        for(j=0; j<cols; j++){
            printf("%d  ", shadows[0][i][j]);
        }
        printf("\n");
}

Everything ok so far. The print of shadows[0] looks fine. Then I pass this matrix to a function like this:

separateMatrixByColumn(1, cols-1, rows, cols, shadows[0], v[0], g[0]);

Inside of that functions the first thing I do is print the matrix shadows:

void separateMatrixByColumn (size_t wanted_cols1, size_t wanted_cols2, size_t rows, size_t columns, int m[rows][columns], int answer1[rows][wanted_cols1], int answer2[rows][wanted_cols2]){

    // this print looks bad. It adds several 0s to the matrix!
    int k;
    int l;
    for (k=0; k<rows; k++){
            for(l=0; l<columns; l++){
                printf("%d  ", m[k][l]);
            }
            printf("\n");
    }

   ...

}

Turns out the print inside of my function looks bad. It shows several 0s inside of my matrix. Why is this happening?


Solution

  • I ended up using an additional amtrix

      int aux[rows][cols];
        for (i=0; i<rows; i++){
                for(j=0; j<cols; j++){
                    aux[i][j] = shadows[0][i][j];
                }
        }
        separateMatrixByColumn(1, cols-1, rows, cols, aux, v[0], g[0]);