i have a problem with input array in function; in this code i take an array parameter with the indexes from user and the function will print the table of 2D array with parameter reserve it; here is the code:
#include <iostream>
#include <windows.h>
using namespace std ;
const char * table[3][2]={{"m1","m2"},{"n1","n2"},{"h1","h2"}} ;
void setTable(char *array,int n,int m) {
for(int i=0 ; i<n;i++){
for(int j=0 ; j<m;j++) {
cout<<array[i][j]<<"---" ; //print array
}
}
}
int main(){
setTable((char * )table,4,2) ; // send array with indexes to function
return 0;
}
but i have a error when i run it:
In function 'void setTable(char*, int, int)':
[Error] invalid types 'char[int]' for array subscript
An array of arrays cant simply be converted to a single pointer.
Generally, when you need to do C-style casting (like in (char * )table
) you should take that as a sign that you do something wrong.
Now to solve your problem... You have to remember that arrays naturally decays to pointers to their first element. That is, table
decay to &table[0]
. This will have the type "pointer to array of two pointers to char
". Or char* (*) [2]
.
So the argument needs to be declared as
char* (*array)[2]
Then you simply pass table
when calling the function:
setTable(table, 3, 2);
If you used standard C++ classes and type-aliases then it would be even simpler:
using table_type = std::array<std::array<std::string, 2>, 3>;
table_type table = { ... };
void setTable(table_type& table) { ... }
And of course I don't recommend using global variables, but if you do use them you don't even need to pass them as arguments to your functions at all.
When passing a C-style 2D array to a function, the function need to know the array dimension in order to do the indexing correctly.
Like:
const char * table[3][2]={{"m1","m2"},{"n1","n2"},{"h1","h2"}} ;
void setTable(const char * array[][2],int n,int m) {
for(int i=0 ; i<n;i++){
for(int j=0 ; j<m;j++) {
cout<<array[i][j]<<"---" ;
}
}
}
int main(){
setTable(table,4,2) ;
return 0;
}
BTW: Consider using the C++ container std::vector
instead of a C-style array
#include <iostream> using namespace std; const char* table[3][2] = {{"m1","m2"},{"n1","n2"},{"h1","h2"}}; template <typename T, int n, int m> void setTable(T (&arr)[n][m]) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << arr[i][j] << "---"; //print array } cout << '\n'; } } int main() { setTable(table); // send array with indexes to function return 0; }
Try renaming "array" to some other word, eg "table". I believe visual studio reserves the word "array".
You need to convert your function argument from char*
to one ofchar**
const char * arr[][2]
: Passing the array by value which would lead to decay to the pointer type for the first dimension(ie of dimension 3). ( reference ) const char * (&arr)[3][2]
: Passing the array by reference will ensure the first dimension does not decay. ( reference ) const char* (*arr)[3][2]
: Passing a pointer to the array itself which preserves the dimensions as the first dimension is already of pointer type. This also requires passing setTable(&arr...)
and dereferencing the pointer by (*arr)[i][j]
while printing.#include <iostream>
// void setTable(const char * arr[][2],int n,int m) {
// void setTable(const char* (*arr)[3][2],int n,int m) { // need to use setTable(&table, 4, 2) and (*arr)[i][j]
void setTable(const char * (&arr)[3][2],int n,int m) {
for(int i=0 ; i<n;i++){
for(int j=0 ; j<m;j++) {
std::cout<<arr[i][j]<<"---" ; //print array
}
std::cout << "\n";
}
}
int main(){
const char * table[3][2]={{"m1","m2"},{"n1","n2"},{"h1","h2"}} ; // 2d array(3x2) of const char*
setTable(table,3,2) ; // send array with indexes to function
return 0;
}
Finally, as mentioned in the other answers using a standard C++ container such as std::array
would be a good idea to avoid the array-to-pointer decay. Additionally, you should avoid using namespace std
.
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.