2

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[][]);
                  ^
Justin
  • 67
  • 6

2 Answers2

7

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Thanks @Sourav, but can you explain to me what you mean by outer index? – Justin Sep 17 '20 at 03:53
  • @Justin Tried adding that illustration into my answer. – Sourav Ghosh Sep 17 '20 at 03:54
  • Ok, I updated the code: I included my .h file as well. I want to be able to create an array and then pass it into the print function to print. The print function doesn't know the # of colmn or rows into it receives the argument as a parameter. How would I write it to reflect that? void print(int arr[][int num]);? – Justin Sep 17 '20 at 04:06
  • Re “to have that adjustment, the type should be known to compiler at compile-time”: No, that is not the reason. It is okay to have a pointer to an i complete type, so adjusting **to** a pointer does not require a complete type. The reason is there is a rule in the C standard that the element type of an array must be a complete type, and there is no exception to this rule for an array parameter that is going to be adjusted. Compilers could actually accept `int a[][]` and adjust it to `int (*)[]` (there is no technical impediment); it is only the rule that prevents it. – Eric Postpischil Sep 17 '20 at 09:58
1

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.

M.M
  • 138,810
  • 21
  • 208
  • 365