警告:从不兼容的指针类型[默认启用]

时间:2016-01-05 13:05:19

标签: c arrays matrix watch

我正在为一个想象中的电视节目写一个节目。这是我的学校项目。

我想在2D阵列中每周举行一次比赛。参赛者和专栏的行数周。当我在Watches窗口中检查矩阵时。一切都好。但是当我尝试编写矩阵时,值是错误的。

“haftalik_top”是我的2D数组的名称。

#define max_matrix 20
#define contestant_max 20
#define week_max 10

在主要功能中;

int haftalik_top[contestant_max][week_max]={{0}};

Here, haftalik_top[ 1 ][ 0 ] = 1 but when i write the matrix. it says zero

haftalik_top[ 1 ][ 0 ]=1 and haftalik_top[ 2 ][ 0 ] = 2 but it says 2 and 0

每次都写错了值。我正在用这个函数编写矩阵:

void matrix_writer(int m[][max_matrix], int row, int column)
{
    int i,j;
    for (i=0;i<row;i++) {
        for (j=0;j<column;j++) {
            printf("%2d ",m[i][j]);
        }
        printf("\n");
    }
}

我正在使用它:

matris_writer(haftalik_top,contestants,weeks);

它在我使用该函数的行上出错。

    warning: passing argument 1 of 'matrix_writer' from incompatible pointer type [enabled by default]

对于我的其他函数调用,我有6个这样的警告。如果此功能出现问题,同样的原因也会导致我的其他功能出错。因为我的其他功能也不能正常工作。谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

您的类型不兼容。 int haftalik_top[contestant_max][week_max]={{0}};是一个长度为20 * 10的二维数组。您的函数void matrix_writer(int m[][max_matrix], int row, int column)需要一个长度为* 20的二维数组。

要么改变你的功能......

void matrix_writer(int m[][week_max], int row, int column)
                        // ^^^^^^^^
{
    ...
}

...或者你改变你的数组:

int haftalik_top[contestant_max][max_matrix]={{0}};
                              // ^^^^^^^^^^

答案 1 :(得分:0)

如评论中所述,发生错误是因为week_max与max_matrix不同。

以下是处理2D数组的常用方法:

#include <stdio.h>

void print_matrix(int *m, int rows, int cols) {
    int row, col;

    for (row = 0; row < rows; row++) {
        for (col = 0; col < cols; col++) {
            printf("%d ", m[row * cols + col]);
        }

        putchar('\n');
    }

    putchar('\n');
}

int main(void) {
    int m1[5][2] = {0}, m2[3][4] = {0};

    print_matrix(&m1[0][0], 5, 2);
    print_matrix(&m2[0][0], 3, 4);

    return 0;
}
相关问题