Home > Article > Backend Development > Why Does C Throw "expected ',' or '...' before '*' token" When Passing a Two-Dimensional Array By Reference?
When attempting to pass a reference to a two-dimensional array to a function in C , users may encounter the error "expected ',' or '...' before '*' token" from g . The question arises: what does this error indicate and how can it be resolved?
The error indicates that the function prototype for do_something() is incorrect. To pass a reference to a two-dimensional array with known dimensions at compile time, the following syntax should be used:
<code class="cpp">void do_something(int (&array)[board_width][board_height]);</code>
Declaring the parameter as int array[board_width][board_height]; would instead pass a pointer to the first sub-array of the array, which is not the desired functionality in this case.
The reference syntax &array ensures that the actual array is passed by reference, allowing the function to modify the array's contents directly. The notation int (&array)[board_width][board_height] specifies that the parameter array is a reference to a two-dimensional array with dimensions of board_width and board_height.
By contrast, int array[board_width][board_height]; declares the parameter array as a pointer to a one-dimensional array of board_height integers, thereby ignoring the second dimension of the array. This results in the reported error because the syntax is incorrect.
The above is the detailed content of Why Does C Throw "expected ',' or '...' before '*' token" When Passing a Two-Dimensional Array By Reference?. For more information, please follow other related articles on the PHP Chinese website!