0

I know there are similar questions, but I still can't figure this out, even though I've been reading for 2 hours.

struct box 
{ 
    char letter; 
    int occupied; //0 false, 1 true. 
}; 

void fill(struct box**, int, int, char*);  //ERROR HERE**


int main(int argc, char** argv) 
{ 
    int length=atoi(argv[4]), 
        width=atoi(argv[5]), 

    char alphabet[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    struct box soup[length][width]; 
    fill(soup, length, width, alphabet);  //HERE**
}

void fill(struct box soup[][], int length, int width, char*alphabet) //AND HERE**
{
   //omitted 
}

These are the errors I'm getting when I compile:

warning: passing argument 1 of ‘fill’ from incompatible pointer type [enabled by default]  
     fill(soup, length, width, alphabet);  
     ^  

note: expected ‘struct box **’ but argument is of type ‘struct box (*)[(sizetype)(width)]’  
void fill(struct box **, int, int, char*);  
     ^  

error: array type has incomplete element type  
void fill(struct box soup[][], int length, int width, char*alphabet)
                     ^

I don't get why that fails, while some other functions I have like this one, does work:

void wordsToMemory(char**, char*);   //prototype
char* dictionary[Nwords];            
wordsToMemory(dictionary, argv[1]);  //calling the method
void wordsToMemory(char* dictionary[], char* argv) //function body
{
 //omitted
}

3 Answers3

0

This will get it to compile:

void fill(struct box** soup, int length, int width, char* alphabet)

or

void fill(struct box* soup[], int length, int width, char* alphabet)

When using [][] you get an error because there is no conversion from struct box* to struct box.

  • You must have a strange compiler that accepts a function argument of type _array of `length` arrays of `width` `struct box`_ for a formal parameter of type _pointer to pointer to `struct box`_ - or you just didn't try to compile it. – Armali Nov 11 '16 at 12:07
0

Array decays into pointers. When you are passing a single dimension array to a function, the function which receives the array can look like this

void fun(char a[10])    void fun(char a[])  void fun(char *a)
{                       {                   {
    ...             OR      ...         OR      ... 
}                       }                   }

Arrays decays into pointer, not always true...Arrays decay into pointers is not applied recursively...Means, 2D Array decays to pointer to array not to pointer to pointer so that is why you are getting the error.

When you are passing 2D array to function, the function which receives the 2D array should look like this...

void fun(char *a[10])
{
    ...
}
  • While the answer's main part is right, the last block isn't; the function rather is `void fun(char (*a)[10])` or `void fun(char a[][10])`. – Armali Nov 11 '16 at 12:14
0
void fill(struct box**, int, int, char*);

This declaration is wrong because it says that the function's first argument has to be of type pointer to pointer to struct box, while you have no objects of type pointer to struct box in main to refer to, rather you have, as you say, a matrix (a two-dimensional array, an array of arrays) of structs.

So, the prototype

void fill(struct box [][], int, int, char *);

is almost correct, except, only the major (the first) dimension of a matrix declaration can be omitted, so we need to specify at least the width therein, which conveniently is also passed to the function, only the parameters' order has to be changed so that width is defined early enough:

void fill(int length, int width, struct box soup[][width], char *alphabet);

The function call in main is consequently:

    fill(length, width, soup, alphabet);
Armali
  • 18,255
  • 14
  • 57
  • 171