c语言函数中多维数组的传递(以多维数组作为参数)
1、 非函数形式
#include <stdio.h> int main(void) { int a[4][3] = {{54,63,14},{65,85,78},{85,74,69},{25,65,78}}; int b[4][3] = {{25,65,74},{85,74,96},{25,87,69},{64,58,87}}; int c[4][3]; int i, j; for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) { c[i][j] = a[i][j] + b[i][j]; } } putchar('\n'); puts("first examination."); for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) { printf("%4d", a[i][j]); } putchar('\n'); } putchar('\n'); puts("second examination."); for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) { printf("%4d", b[i][j]); } putchar('\n'); } putchar('\n'); puts("examination sum."); for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) { printf("%4d", c[i][j]); } putchar('\n'); } return 0; }
2、函数形式
#include <stdio.h>
void func1(const int x[4][3], const int y[4][3], int z[4][3])
{
int i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j< 3; j++)
{
z[i][j] = x[i][j] + y[i][j];
}
}
}
void func2(const int x[4][3])
{
int i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
{
printf("%4d", x[i][j]);
}
putchar('\n');
}
}
int main(void)
{
int i, j;
int a[4][3] = {{85,45,69},{36,45,87},{45,69,34},{54,78,96}};
int b[4][3] = {{58,46,24},{78,36,59},{21,46,97},{58,79,63}};
int c[4][3];
func1(a, b, c);
putchar('\n');
puts("first examination.");
func2(a);
putchar('\n');
puts("second examination.");
func2(b);
putchar('\n');
puts("examination sum.");
func2(c);
return 0;
}
多维数组的传递(多维数组作为参数):和数组的传递不一样,多维数组的传递在形参声明中需要注明各个维度的元素数目。例如:数组传递: int func1(int x[], int y). 多维数组传递: int func1(int x[4][3]).