1

If i will declare array in the following way:

#define N 10

char board[N][N]

And I want to write function void read_input(...) that will get the array board, what I need to write instead ... ?

I don't sure what it's need to be (maybey char** or char[N][N]? or else something?)

dbush
  • 205,898
  • 23
  • 218
  • 273
Mathing
  • 41
  • 5
  • 1
    The type of `char board[N][N]`, is, unsurprisingly, `char[N][N]`. If you're asking about the type of a pointer `char[N][N]` decays to, then the answer is `char(*)[N]` (note that's it's the *left* `[N]` that's removed). You could pass it as `char x[N][N]` or `char (*x)[N]` (the first one is no different from and is automatically converted to the second one). – HolyBlackCat Dec 26 '18 at 16:33
  • 1
    @HolyBlackCat: And the function definition and prototype could also use `char board[][N]` for the parameter. – Jonathan Leffler Dec 26 '18 at 16:35

1 Answers1

-1

Since the array size is a compile time constant, you can denote the array with that size when it is passed to your function.

void read_input(char board[N][N])
{
   ...
}


read_input(board);
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    and if `N` is defined in the following way `#define N 10` ? – Mathing Dec 26 '18 at 16:22
  • In that case, since `N` is a macro representing a compile time constant, you can simply pass in the array specifying the size: `read_input(char board[N][N])` – dbush Dec 26 '18 at 16:24
  • @Mathing: if the dimensions are constant, you don't have to use the `*` notation; that is for variable-length arrays. – Jonathan Leffler Dec 26 '18 at 16:24
  • @dbush can you edit your answer in according to this case? – Mathing Dec 26 '18 at 16:25
  • `cha board[*][*]` will not compile, the `char [*]` is an incomplete type, you can't make an array of incomplete types. – KamilCuk Dec 26 '18 at 16:28
  • @Mathing Updated to reflect the updated requirement. – dbush Dec 26 '18 at 16:29
  • 1
    @KamilCuk `void read_input(int, char board[*][*]);` does in fact compile. As a function declaration, it means that the dimensions of the array are defined by parameters specified earlier. – dbush Dec 26 '18 at 16:30
  • @KamilCuk, `char board[*][*]` is fine (for a function parameter), provided that the implementation supports VLAs and is conforming. – John Bollinger Dec 26 '18 at 16:31
  • 1
    @KamilCuk: You can use the `char array[*][*]` notation in function declarations to indicate that the dimensions are determined at runtime. I don't think you can use it in the function definition, though. See C11 [§6.7.6.3 Function declarators (including prototypes)](http://port70.net/~nsz/c/c11/n1570.html#6.7.6.3), especially [¶12](http://port70.net/~nsz/c/c11/n1570.html#6.7.6.3p12]). – Jonathan Leffler Dec 26 '18 at 16:31