Home > Backend Development > C++ > body text

Why Does C Throw 'expected ',' or '...' before '*' token' When Passing a Two-Dimensional Array By Reference?

Linda Hamilton
Release: 2024-11-06 00:41:02
Original
1006 people have browsed it

Why Does C   Throw

Passing a Reference to a Two-Dimensional Array

Problem

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?

Solution

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>
Copy after login

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.

Explanation

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!