Array has incomplete element type. What does this mean? [duplicate]

旧街凉风 提交于 2021-02-17 05:01:48

问题


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:

My code:

This is my .c file:

 #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]);
        }
        
    }

This is my header file:

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

The Error:

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

回答1:


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

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

or, for more dimensions

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

The analogy behind this is, for function declarators,

"A declaration of a parameter as ''array of type'' shall be adjusted to ''qualified pointer to type'',...."

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

In case of a declaration like

void print(int arr[][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

The function definition is

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

but actually you do

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

this is invalid because of two things

  • 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.

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




回答2:


To fix the printArray function you will need to include the array dimensions. In C, arrays are simply a block of elements, there is no length stored anywhere. The programmer would need to keep track of the length by some method.

For example:

// header
void printArray(int num, int arr[num][num]);

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

For the multiplication function you will need to do something similar, the caller should allocate the array (e.g. int arr[num][num];) and then the function should fill in the values for the array cells.



来源:https://stackoverflow.com/questions/63930872/array-has-incomplete-element-type-what-does-this-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!