传递三维数组作为参数传递给C中的函数 [英] Passing 3D array as parameter to a function in C

查看:305
本文介绍了传递三维数组作为参数传递给C中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去编写一个函数,总结一个三维数组的元素,但它口口声声说没有用,我通过三维数组作为参数的线有问题。我的code是如下:

 的#include< stdio.h中>

INT sum3darray(一[] [] [],大小);

主要() {
    INT检查[3] [3] [3] = {0};
    INT大小= 3;
    的printf(之和为%d \ N,sum3darray(支票,大小));
}

INT sum3darray(一[] [] [],大小){
    INT I,J,K,总和= 0;
        对于(i = 0; I<大小;我++){
            为(J = 0; J<大小; J ++){
                对于(K = 0; K<大小; k ++){
                    输出(I =%D,J =%D,K =%D,checkijk =%D,I,J,K,选中[I] [J] [K]);
                    总和+ =检查[i] [j]的[K]
                    的printf(总和=%D \ N,总和);
                }
            }
        }
返回(总和);
}
 

编译器标志线3和11的问题。任何人都可以帮忙吗?

解决方案
  1. 您不能省略类型
  2. 您可以省略数组
  3. 只有第一个维度

  INT sum3darray(一[] [] [],大小);
 

  INT sum3darray(INT A [] [3] [3],诠释大小);
 

  INT sum3darray(INT(*一)[3] [3],诠释大小);
 

正如刚才@CoolGuy,你可以省略原型的参数的名称:

  INT sum3darray(INT(*一)[3] [3],诠释大小);
 

可以写成

  INT sum3darray(INT(*)[3] [3],INT);
 

另一种方式来处理多维数组(如果你不知道事先尺寸)正在使用VLA的(谢谢你米Oehm)或手动压平的数组,的看看

Im trying to write a function that sums the elements of a 3d array, however it keeps saying there is a problem with the lines where I pass the 3d array as a parameter. My code is as follows:

#include <stdio.h>

int sum3darray(a[][][], size);

main() {
    int check[3][3][3]={ 0 };
    int size=3;
    printf("The sum is %d\n",sum3darray(check,size));
}

int sum3darray(a[][][],size) {
    int i,j,k,sum=0;
        for(i=0;i<size;i++) {
            for(j=0;j<size;j++) {
                for(k=0;k<size;k++) {
                    printf("i=%d, j=%d,k=%d, checkijk=%d  ",i,j,k,check[i][j][k]);
                    sum+=check[i][j][k];
                    printf("sum=%d\n", sum);
                }
            }
        }
return(sum);
}

The compiler flags lines 3 and 11 as problems. Can anyone help?

解决方案

  1. You can not omit the types
  2. You can omit only the first dimension of an array


int sum3darray(a[][][], size);

should be

int sum3darray(int a[][3][3], int size);

or

int sum3darray(int (*a)[3][3], int size);

As pointed out by @CoolGuy, you can omit the name of the parameters in prototypes:

int sum3darray(int (*a)[3][3], int size);

can be written as

int sum3darray(int (*)[3][3], int);

Another way to deal with multidimensional arrays (if you don't know the sizes beforehand) is using VLA's (thank you M Oehm) or flattening the array manually, take a look.

这篇关于传递三维数组作为参数传递给C中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆