计算矩阵总和的函数给出错误-[错误]数组类型的元素类型不完整 [英] Function to calculate sum of matrix gives error - [Error] array type has incomplete element type

查看:89
本文介绍了计算矩阵总和的函数给出错误-[错误]数组类型的元素类型不完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计算矩阵总和的函数给出以下错误-[错误]数组类型的元素类型不完整

Function to calculate sum of matrix gives following error - [Error] array type has incomplete element type

任何人都可以解释我在做什么。

Could anyone explain what I'm doing wrong.

这是代码:

  #include<stdio.h>

  int some_tab(int t[][],int n,int m);
  int main(){

    int tab[100][100],i,j,n,m;

    printf("\ndonner le nombre de lignes : ");

    scanf("%d",&n);
    printf("\ndonner le nombre des columns : ");
    scanf("%d",&m);

    for(i=0 ; i<n  ;i++){  
      printf("\nLigne %d: \n",i+1);
      for(j=0 ; j<m ;j++){  
          printf("\n donner le nombre numero %d :  ",j+1);
          scanf("%d",&tab[i][j]);   

       }
     }      

    printf("\nla somme du tableau est %d :  ",some_tab(tab,n,m));

    return 0;
  }

  int some_tab(int t[][],int n,int m){

        int i,j,s=0;

        for(i=0;i<n;i++){  
            for(j=0;j<m;j++)                                 
                 s = s + t[i][j];                                 
         }
        return s;
    }


推荐答案

当您传递一个数组时具有多个维度作为函数的参数,则只有 first (最左侧)维度可以具有未知大小(空 [] )。这是必需的,因为给定元素的地址的计算方式。

When you pass an array that has more than one dimension as an argument to a function, then only the first (leftmost) dimension can have an unknown size (empty []). This is required because of the way the address of a given element is calculated.

例如,在以下代码中:

char array[10][20];
char b = array[3][2];

变量 array 占用连续的内存块,长度为200(10 x 20)字节。元素 array [0] [0] 的地址(显然)将与数组的基地址相同。为了获得 array [1] [0] 的地址,编译器需要在该基址上添加一个值:在这种情况下,该值将是第二维度的大小(20);同样,对于 array [2] [0] ,地址将是第二维尺寸的两倍,依此类推。

The variable array occupies a contiguous chunk of memory that is 200 (10 x 20) bytes in length. The address of element array[0][0] will be (obviously) the same as the array's base address. To get the address of array[1][0], the compiler needs to add a value to that base address: in this case, that value will be the size of the second dimension (20); similarly, for array[2][0], the address will be two times the size of the second dimension, and so forth.

然后我们开始更改 second 索引时,只需要将该索引的实际值添加到第一个索引的计算出的偏移量中即可。因此,如果我们将数组的基地址称为 ADDR ,则在上面的示例中, b 的值为 char ADDR + 20 x 3 + 2

When we then start changing the second index, we need only add that index's actual value to the calculated 'offset' for the first index. Thus, if we call the array's base address ADDR then, in the example above, b will be given the value of the char at ADDR + 20 x 3 + 2.

但是,在您的 some_tab 函数中,编译器不知道 t 数组是-那么如何计算用于元素 t [i] [j] 的适当地址偏移量? (请注意,编译器可以应付不知道第一维的大小的问题,因为该大小从未用于计算元素的地址!)

However, inside your some_tab function, the compiler doesn't know what the size of the second dimension of the t array is - so how can it calculate the appropriate address offset to use for the element t[i][j]? (Note that the compiler can cope with not knowing the size of first dimension, because that size is never used in calculating the address of an element!)

,在您的情况下,最简单(尽管不一定是 best )方式是为第二个维度赋予固定大小: )足够大,可以处理代码必须处理的所有情况;和(b)在整个代码中始终使用 100% 。因此,您可以使用以下代码:

To get round this, in your case, the simplest (though not necessarily the best) way is to give the second dimension a fixed size that is: (a) big enough to handle all cases the code will have to deal with; and (b) used 100% consistently throughout your code. So, you could use this:

int some_tab(int t[][100], int n, int m);

int main {
    int tab[100][100], i, j, n, m;
    //..
    printf("\nla somme du tableau est %d :  ", some_tab(tab, n, m));

但是,以下内容将严重失败,因为它会破坏上面的(b)点:

However, the following will fail horribly because it breaks point (b), above:

int some_tab(int t[][200], int n, int m); // Change 100 to 200 for bigger space?

int main {
    int tab[100][100], i, j, n, m; /// This is now WRONG because tab has different 2nd dimension size!
    //..
    printf("\nla somme du tableau est %d :  ", some_tab(tab, n, m));

*注意:我使用了 char 数组,以使说明简短,因为 char 类型的大小为恰好 1个字节。对于其他类型,任何元素的计算出的偏移量都将简单地乘以该类型的大小。*

*Note: I have used a char array here to keep the explanation brief, as the char type has a size of exactly 1 byte. For other types, the calculated offset for any element would simply be multiplied by the size of that type.*

编辑:当我第一次发布内容时,我无法测试(更好)的代码,因为我被限制使用MSVC本机 C 编译器(该编译器使用了非常老的语言标准禁止使用它)。但是,既然我已经可以使用 clang 进行测试,则可以确认以下代码可以按预期进行编译和运行。

When I first posted, I couldn't test the (better) code that follows, as I was 'restricted' to using the MSVC native C compiler (which uses a very old language standard that prohibits it). However, now that I have been able to test with clang, I can confirm that the following code compiles and works as expected.

此操作是对数组使用已知 变量 的维,这些维将传递给在运行时运行some_tab 函数(编译器仍可以正确计算每个元素的偏移量,但是它没有使用 fixed 乘数作为第二维,而是使用了变量(在这种情况下,为参数 m 的值)。

What this does is to use known but variable dimensions for an array, which are passed to the some_tab function at run-time (the compiler can still calculate each element's offset correctly but, rather than using a fixed multiplier for the second dimension, it uses a variable (in this case, the value of the argument m).

请务必阅读我添加的注释,用三斜杠(///)标记:

Make sure to read the comments I have added, marked with the triple-slash (///):

#include<stdio.h>

int some_tab(int n, int m, int t[n][m]); /// We MUST pass the "n" and "m" arguments FIRST!
int main()
{
    int i, j, n, m; /// NOTE: Declaration of "tab" MUST be moved ...
    printf("\ndonner le nombre de lignes : ");
    scanf("%d", &n);
    printf("\ndonner le nombre des columns : ");
    scanf("%d", &m);
    int tab[n][m]; /// ... to AFTER we know the actual values of "n" and "m"!!
    for (i = 0; i < n; i++) {
        printf("\nLigne %d: \n", i + 1);
        for (j = 0; j < m; j++) {
            printf("\n donner le nombre numero %d :  ", j + 1);
            scanf("%d", &tab[i][j]);
        }
    }
    printf("\nla somme du tableau est %d :  ", some_tab(n, m, tab)); /// Change argument order!
    return 0;
}

int some_tab(int n, int m, int t[n][m])  /// Note that this matches the pre-declaration!
{
    int i, j, s = 0;
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++)
            s = s + t[i][j];
    }
    return s;
}

请随时要求进一步的澄清和/或解释。

Please feel free to ask for any further clarification and/or explanation.

这篇关于计算矩阵总和的函数给出错误-[错误]数组类型的元素类型不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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