Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing 2d array char into function

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);
    }
like image 407
hatfieldac Avatar asked Nov 02 '12 23:11

hatfieldac


2 Answers

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);
}
like image 183
0x90 Avatar answered Oct 14 '22 10:10

0x90


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.

like image 22
LSerni Avatar answered Oct 14 '22 10:10

LSerni