0

Basically, in the code below I have a function which initialises a two-dimensional array. In the main function I try to test the function with different sizes of the array. However, I get the following errors: "error: array type has incomplete element type void chessBoard(char board[][], int length, int width)" and "error: type of formal parameter 1 is incomplete". chessBoard(board1, length1, width1);. ANy suggestions?

    #include <stdio.h>
    #include <stdlib.h>

    void chessBoard(char board[][], int length, int width)
    {
      for (int i = 0; i < length; i++)
      { 
        for (int j = 0; j < width; j++)
          if ((i + j) % 2 == 0)
            board[i][j] = 'b';
          else 
            board[i][j] = 'w';
      }
    }

    int main()
    {
      char board1 [3][4];
      int length1 = sizeof board1 / sizeof board1[0];
      int width1 = sizeof board1[0] / sizeof(int);
      chessBoard(board1, length1, width1);
      int i, j;
      for(i = 0; i < length1; i++)
      {
        for(j = 0; j < width1; j++)
          printf("%c", board1[i][j]);

        printf("\n");
      }
      printf("\n");
}
2
  • void chessBoard(char board[][], int length, int width) --> void chessBoard(int width, char board[][width], int length) , sizeof(int) --> sizeof(char) Commented Oct 15, 2016 at 15:01
  • The thing with arrays in function arguments is that they aren't really arrays, but pointers. Even if you declare an argument like int arr[] the compiler treats it as int *arr. Commented Oct 15, 2016 at 15:01

2 Answers 2

1

You should declare your function like this:

void chessBoard(char board[][4], int length, int width)

This C FAQ thoroughly explains why. The gist of it is that arrays decay into pointers once, it doesn't happen recursively. An array of arrays decays into a pointer to an array, not into a pointer to a pointer. According to the C standard, you can pass any dimensional array to functions as long as you specify all the dimensions as constants except first one (which can be blank).

Sign up to request clarification or add additional context in comments.

2 Comments

With C99 (and C11 if the compiler supports VLAs — variable length arrays), the array dimensions do not have to be constants. You can use: void chessBoard(int length, int width, char board[length][width]) and similar definitions.
"Declaring the function like this" will freeze the second size at 4. This is obviously not what the OP needs.
0

you can also declare your function like this.void chessBoard(char board[][width], int length). int widthis global variable.

#include <stdio.h>
#include <stdlib.h>
int width;//Global Variable
void chessBoard(char board[][width], int length){
    int i,j;
    for ( i = 0; i < length; i++){
        for ( j = 0; j < width; j++){
            if ((i + j) % 2 == 0)
                board[i][j] = 'b';
            else
                board[i][j] = 'w';
        }//inner loop end
    }//outer loop end
}
int main(){
    char board1[3][4];
    int length1 = sizeof(board1) / sizeof(board1[0]);
    int width1 = sizeof(board1[0]) / sizeof(board1[0][0]);
    width = width1;
    chessBoard(board1, length1);
    int i, j;
    for(i = 0; i < length1; i++){
        for(j = 0; j < width1; j++)
            printf("%c ", board1[i][j]);
            printf("\n");
    }
    printf("\n");
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.