0

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
Nima
  • 28
  • 8

5 Answers5

3

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.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
2

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

4386427
  • 33,845
  • 4
  • 32
  • 53
1
#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;
}
d_kog
  • 133
  • 6
-1

Try renaming "array" to some other word, e.g. "table". I believe visual studio reserves the word "array".

OneKneeToe
  • 55
  • 5
-1

You need to convert your function argument from char* to char** one of

  1. const char * arr[][2]: Passing the array by value which would lead to decay to the pointer type for the first dimension(i.e. of dimension 3). (reference)
  2. const char * (&arr)[3][2]: Passing the array by reference will ensure the first dimension does not decay. (reference)
  3. 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;
}

Code Link Related Reading

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.

tangy
  • 2,330
  • 1
  • 16
  • 34