【问题标题】:Matrix of type int using uint8_t data prints wrong when passed to a function使用 uint8_t 数据的 int 类型矩阵在传递给函数时打印错误
【发布时间】:2019-10-24 07:32:17
【问题描述】:

好的,所以我得到了以下指针 sh 和矩阵阴影数组。

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

我这样填充阴影:

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++;
        }
}

然后我可以像这样打印例如 shadows[0]:

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

到目前为止一切正常。 shadows[0] 的打印效果看起来不错。 然后我将这个矩阵传递给这样的函数:

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

在该函数内部,我要做的第一件事是打印矩阵阴影:

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");
    }

   ...

}

原来我的函数内部的打印看起来很糟糕。它在我的矩阵中显示了几个 0。为什么会这样?

【问题讨论】:

  • separateMatrixByColumn 的声明参数列表中,您有int m[rows][columns],这是不对的。 rowscolumns 需要匹配声明中的值,而不是您实际使用的元素的值。所以它们需要是 320 和 210。(实际上第一个维度可以更小,但之后的任何内容都必须完全匹配。)其他数组可能有类似的问题。
  • 谢谢,就是这样。我让我的矩阵更大 bcI 我有两种可能的尺寸。无论如何,我使用辅助矩阵。

标签: c function matrix int uint8t


【解决方案1】:

我最终使用了一个额外的 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]);

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多