数组的元素类型不完整.这是什么意思? [英] Array has incomplete element type. What does this mean?

查看:74
本文介绍了数组的元素类型不完整.这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个数组,该数组存储任何整数n的乘法值.之后,我想将该数组传递给另一个函数并打印出该数组.但是,出现以下错误:

I want to create an array that has that stores the multiplication values of any integer n. After that, I would like to pass that array into another function and print out the array. However, I get the following error:

我的代码:

这是我的.c文件:

 #include "multiplication.h"
#include <stdio.h> 
int main(){
int num;
int arr=multiplication(4);
printArray(arr);



}

int mulitpication(int num){
 /* initialize array and build*/
    int arr[num][num];
    for(int i=0; i<num;i++){
        printf("row number: %d ",i);
         for(int j=0;j<num;j++){
             printf("column number: %d", j);
            arr[i][j]= (i+1)*(j+1);
         }
    }
    return arr;
    }

    void printArray(int arr[][]){
    
    int i;
    for(i=0;i<sizeof(arr);i++){
        for(int j=0;j<sizeof(arr);j++){
            printf("%d ",arr[i][j]);
        }
        
    }

这是我的头文件:

void multiplication(int num);
void print(int arr[][]);

错误:

 multiplication.h:4:19: error: array has incomplete element type 'int []'
void print(int arr[][]);
                  ^

推荐答案

首先,您不将源文件相互包含,而是将它们编译并链接在一起以形成二进制文件.

First of all, you don't include the source files into one another, you compile and link them together to form the binary.

也就是说,实际的问题出在您未显示的代码中( multiplication.h 文件),但是从错误消息中我们可以看到

That said, the actual problem is in the code you did not show (multiplication.h file), but from the error message we can see

 void print(int arr[][]);

不是有效的语法.您只能将最外面的索引保留为空,所有其他索引必须具有适当的值.像

is not a valid syntax. You can only leave the outer(-most) index as empty, all other index(es) must have a proper value. Something like

void print(int arr[ ][10]);
                      ^^---------- inner index
                 ^^^------------- outer index

或者,获取更多尺寸

    void print(int arr[ ][5][10][15]);

对于函数声明符,这背后的类比是

The analogy behind this is, for function declarators,

将参数声明为类型数组"调整为类型的合格指针",...."

因此,要进行调整,编译器应该在编译时就知道 type .

So, to have that adjustment, the type should be known to compiler at compile-time.

在类似这样的声明

void print(int arr[][10]);

类型为 int [10] ,但如果语法类似

the type is int[10], but if a syntax like

    void print(int arr[][]);     

允许

,类型未知.因此是错误.

is allowed , the type cannot be known. Hence the error.

其他问题:您似乎还有很多其他问题,例如

Other issues: You seem to have many other issues, like

函数定义为

int mulitpication(int num){  // returning an int

但实际上你是

return arr;  //where arr is an array of size int[num][num], defined locally

这是无效的,因为有两点原因

this is invalid because of two things

  • int int [num] [num] 的类型不同.
  • VLA的范围(即 arr )仅限于功能块,您不能让数组将地址返回给调用方,并且期望有意义的东西,因为返回的地址将不再有效.
  • an int and an int[num][num] are not the same type.
  • the scope of a VLA i.e., arr is limited to the function block, you cannot have the array return the address to the caller and expect something meaningful as the returned address will not be valid anymore.

我相信,最好使用分配的内存( malloc()和family)并手动跟踪索引/元素计数.

I believe, you're better off using allocated memory (malloc() and family) and keeping track of your index/ count of elements manually.

这篇关于数组的元素类型不完整.这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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