I'm getting back into c++ and am having trouble figuring out how to pass a 2D-array to a function. The code below is my current attempt, I've been able to pass vector strings by reference by using:
vector<string> g_dictionary;
getDictionaryFromFile(g_dictionary, "d.txt");
...
void getDictionaryFromFile(vector<string> &g_dictionary, string fileName){..}
But when I try to do the same thing with my 2d-array like so below, I get an error on the line "solve_point(boardEx);"said a reference of type char & cannot be initialized with a value of type boardEx[5][4]
#include <stdio.h>
#include <string>
using namespace std;
void solve_point(char* &board){
printf("solve_point\n");
//board[2][2] = 'c';
}
int main(){
char boardEx[5][4];
solve_point(boardEx);
}
The type char*&
is a reference to a pointer. A "2d" array decays to a pointer to an array.
For your array boardEx
it will decay to the type char(*)[4]
which needs to be the type your function accepts:
void solve_point(char (*board)[4]) { ... }
Or you can use templates to deduce the array dimensions
template<size_t M, size_t N>
void solve_point(char (&board)[M][N]) { ... }
Or use std::array
:
std::array<std::array<char, 5>, 4> boardEx;
...
void solve_point(std::array<std::array<char, 5>, 4> const& board) { ... }
Or use std::vector
:
std::vector<std::vector<char>> boardEx(5, std::vector<char>(4));
...
void solve_point(std::vector<std::vector<char> const& board) { ... }
Considering the edit of the question, the solution using std::vector
is the only portable and standard solution possible.
A reference to a 2D array like you have can be defined using:
char (&ref)[5][4] = boardEx;
You can change the function to use the same syntax.
void solve_point(char (&board)[5][4]){
printf("solve_point\n");
//board[2][2] = 'c';
}
For a dynamically allocated array, it will be better to use std::vector
.
int width = 7;
int height = 9;
char boardEx[width][height];
is supported by some compilers as an extension but it is not standard C++. Instead, use:
int width = 7;
int height = 9;
std::vecotr<std::vector<char>> boardEx(width, std::vector(height));
Update solve_point
accordingly.
You can declare the function that accepts the array either by value or by reference.
For example (by value)
void solve_point( char ( *board )[4] ){
printf("solve_point\n");
//board[2][2] = 'c';
}
int main(){
char boardEx[5][4];
solve_point(boardEx);
}
Or (by reference)
void solve_point(char ( &board )[5][4] ){
printf("solve_point\n");
//board[2][2] = 'c';
}
int main(){
char boardEx[5][4];
solve_point(boardEx);
}
In the both cases you can access an element of the array using an expression like this
board[i][j] = 'c';
Bear in mind that if you have a multidimensional array like for example this
T a[N1][N2][N3];
where T
is some type specifier then you can rewrite the declaration the following way
T ( a[N1] )[N2][N3];
Now to get pointer to elements of the array just substitute ( a[N1] )
for example for ( *pa )
T ( *pa )[N2][N3] = a;
To get reference to the array rewrite its declaration like
T ( a )[N1][N2][N3];
and substitute ( a )
for ( &ra )
like
T ( &ra )[N1][N2][N3] = a;
If you want to write a function that accepts two dimensional arrays of different sizes by reference then you can write
template <typename T, size_t M, size_t N>
void solve_point( T ( &board )[M][N] ){
//...
}
int main(){
char boardEx[5][4];
solve_point(boardEx);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.