定义和打印矩阵所有元素的函数

Function for defining and printing all the elements of a matrix

我正在研究 C 中的函数,我想创建一个函数来为矩阵的所有元素设置相同的值,然后用另一个函数打印矩阵。可能吗?这是我写的代码[编辑]:

#include <stdio.h>
#include <stdlib.h>
#define x 79
#define y 24

int def_matrice(int matrice[y][x], int valore);
int print_matrice(int matrice[y][x]);

int main()
{
    int  i = 0, j = 0, matrice[y][x];

    for(i = 0; i < y; i++)
    {
        for(j = 0; j < x; j++)
        {
            matrice[i][j] = 32;
        }
    }

    for(i = 0; i < y; i++)
    {
        for(j = 0; j < x; j++)
        {
            printf("%c", matrice[i][j]);
        }
        printf("\n");
    }

    def_matrice(matrice[y][x], 89);
    print_matrice(matrice[y][x]);

    return 0;
}

int def_matrice(int matrice[y][x], int valore)
{
    int i = 0, j = 0;
    for(i = 0; i < y; i++)
    {
        for(j = 0; j < x; j++)
        {
            matrice[i][j] = valore;
        }
    }
    return 0;
}

int print_matrice(int matrice[y][x])
{
    int i = 0, j = 0;
    for(i = 0; i < y; i++)
    {
        for(j = 0; j < x; j++)
        {
            printf("%c", matrice[i][j]);
        }
        printf("\n");
    }
    return 0;
}

但是当我尝试编译时,出现以下错误[编辑]:

|30|error: passing argument 1 of 'def_matrice' makes pointer from integer without a cast|
|6|note: expected 'int (*)[79]' but argument is of type 'int'|
|31|error: passing argument 1 of 'print_matrice' makes pointer from integer without a cast|
|7|note: expected 'int (*)[79]' but argument is of type 'int'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

修复原型(在函数声明和函数定义处):

int def_matrice(int matrice[y][x], int valore)
int print_matrice(int matrice[y][x])

然后修正你的函数调用:

def_matrice(griglia, 89);
print_matrice(griglia);

那么您需要在 def_matriceprint_matrice.

中声明 ij

然后 def_matriceprint_matrice 的参数名称为 matrice 但函数中使用了名称 griglia