【发布时间】:2015-04-21 20:41:10
【问题描述】:
我正在研究 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;
}
但是当我尝试编译时,我得到以下错误[EDITED]:
|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 matrice[int y][int x]? -
无处,我试过了,因为在我的书中没有这种类型的例子,只是简单的算术运算。
标签: c function for-loop matrix nested-loops