8

I'm trying to pass a 2D array with strings in it into a function. I keep getting the expected expression before ] token.

The point of this code is to read in a wordsearch puzzle, then find the words in that puzzle. I'm going to be writing a function for forward search, backword search, and then up and down search.

How do I get rid of this error? The error is down at the very bottom where I call the forward function.

    /*Andrea Hatfield CPE 101 October 31st, 2012*/
    #include <stdio.h>
    #include <string.h>

    int forward(char words[][8], char puzzle[][11]);


    int main()
    {
    char puzzle[11][11];
    char words[8][8];
    FILE *fin, *fwords;
    int i = 0;
    int j= 0;

    fin = fopen("puzzle.in", "r"); 
    fwords = fopen("words.in", "r");


    if(fin == NULL) /*Reads in the puzzle file*/
    printf("File does not exist");
    else
    {
            while(fscanf(fin,"%s", puzzle[i])!=EOF)
            {
                    printf("%s\n", puzzle[i]);
                    i++;
            }


    }

    if(fwords == NULL) /*Reads in the words the puzzle will search for */
    printf("File does not exist");
    else
    {
            while(fscanf(fwords, "%s", words[j])!=EOF)
            {
                    printf("%s\n", words[j]);
            }
    }


    forward(&words[][8], &puzzle[][11]); /*Error at this point*/
    return(0);
    }
1
  • Welcome to Stackoverflow, can you please provide a clear question?
    – Pepe
    Nov 2, 2012 at 23:05

2 Answers 2

5

It is a little bit hard to get what you want to do but there is a problem in here:

forward(&words[][8], &puzzle[][11]);

try this instead:

forward(words, puzzle);

Here are two other options that should be mentioned from here:

#define ROWS 4
#define COLS 5

void func(int array[ROWS][COLS]) {
  int i, j;

  for (i=0; i<ROWS; i++) {
    for (j=0; j<COLS; j++) {
      array[i][j] = i*j;
    }
  }
}

void func_vla(int rows, int cols, int array[rows][cols]) {
  int i, j;

  for (i=0; i<rows; i++) {
    for (j=0; j<cols; j++) {
      array[i][j] = i*j;
    }
  }
}

int main() {
  int x[ROWS][COLS];

  func(x);
  func_vla(ROWS, COLS, x);
}
0
3

You need to call directly

 forward(words, puzzle);

However, you have an error in your code: you forgot the j++ in the reading cycle while reading words, so you are overwriting words[0] and not initialising the other members of the array.

Also, in a real world situation, you might find it desirable to choose the array size at run time. A simple way to do it would be to choose a reasonably large limit for both width and height, and go with 0x90's solution (you need a 30x20 puzzle, and you can handle it easily since you compiled with ROWS and COLS equal to, say, 1024).

A more complicated way would be to use malloc() (using pointers to pointers to chars) and allocate both arrays dynamically, limited by available memory; you would then pass their dimensions to the forward function.

int forward(char **words, char **puzzle, size_t w_w, size_t w_h, size_t p_w, size_t p_h)
{

}

Yet, the allocation part would be more complicated given the looped calls to malloc() and the need to check its return value every time to intercept out-of-memory conditions; also, in some scenarios you might want the memory to be deallocated (e.g. to run repeated puzzles), which leads to further complexity.

4
  • Disagree with the second half of this post, that makes the code considerably more complicated for no reason. The original design is fine. You can pass the array dimensions as extra parameters if desired.
    – M.M
    Oct 16, 2016 at 23:23
  • I'm sorry, @M.M , but... isn't that what I suggested? supply them from outside meant exactly that, pass the dimensions as extra parameters. Since there are two arrays (words and puzzle), I need four extra parameters. I'll admit that I've allowed for rectangular puzzles; for square ones you need only half the parameters :-)
    – LSerni
    Oct 16, 2016 at 23:29
  • 1
    You're also suggesting changing to passing a pointer-to-pointer and using malloc (which I'm not, just passing the dimensions and retaining the existing method of allocating the array)
    – M.M
    Oct 16, 2016 at 23:31
  • @M.M , I see. Yes, that might be considered additional complexity. It hadn't crossed my mind. I'll reword the answer accordingly; thanks!
    – LSerni
    Oct 16, 2016 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.