【问题标题】:2d array with index input function in c++c++中带有索引输入函数的二维数组
【发布时间】:2021-03-17 19:14:23
【问题描述】:

函数中的输入数组有问题; 在这段代码中,我从用户那里获取一个带有索引的数组参数,该函数将打印带有参数保留的二维数组表; 这是代码:

   #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;
     }

但是当我运行它时出现错误

    In function 'void setTable(char*, int, int)':
    [Error] invalid types 'char[int]' for array subscript

【问题讨论】:

  • 你使用原始数组而不是容器类有什么原因吗?

标签: c++ arrays function multidimensional-array


【解决方案1】:

尝试将“array”重命名为其他单词,例如“桌子”。我相信visual studio保留了“数组”这个词。

【讨论】:

    【解决方案2】:

    您需要将函数参数从char* 转换为char** 之一

    1. const char * arr[][2]:按值传递数组,这将导致衰减到第一个维度(即维度 3)的指针类型。 (reference)
    2. const char * (&amp;arr)[3][2]:通过引用传递数组将确保第一个维度不会衰减。 (reference)
    3. const char* (*arr)[3][2]:传递一个指向数组本身的指针,它保留了维度,因为第一个维度已经是指针类型。这还需要在打印时传递 setTable(&amp;arr...) 并通过 (*arr)[i][j] 取消引用指针。
    #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 LinkRelated Reading

    最后,正如其他答案中提到的那样,使用标准 C++ 容器(例如 std::array)将是避免数组到指针衰减的好主意。另外,你应该avoidusing namespace std

    【讨论】:

    • 不要将您的答案作为链接发布
    • 是的,你的权利 - 这个答案不正确 - 一旦 OP 不接受它,我将删除它。
    • 我会假设 OP 永远不会不接受您的答案并尝试修复它。否则,您可能会为您甚至不想要的答案累积更多的反对票。
    【解决方案3】:

    将 C 样式的二维数组传递给函数时,函数需要知道数组维度才能正确进行索引。

    喜欢:

    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;
    }
    

    顺便说一句:考虑使用 C++ 容器 std::vector 而不是 C 样式数组

    【讨论】:

      【解决方案4】:

      数组数组不能简单地转换为单个指针。

      通常,当您需要进行 C 风格的转换时(例如在 (char * )table 中),您应该将其视为您做错了什么的标志。

      现在要解决您的问题...您必须记住,数组自然会衰减为指向其第一个元素的指针。也就是说,table 衰减为&amp;table[0]。这将具有“指向char 的两个指针的数组的指针”类型。或char* (*) [2]

      所以参数需要声明为

      char* (*array)[2]
      

      那么你在调用函数时只需传递table

      setTable(table, 3, 2);
      

      如果您使用标准 C++ 类和类型别名,那就更简单了:

      using table_type = std::array<std::array<std::string, 2>, 3>;
      
      table_type table = { ... };
      
      void setTable(table_type& table) { ... }
      

      当然我不推荐使用全局变量,但如果你确实使用它们,你甚至不需要将它们作为参数传递给你的函数。

      【讨论】:

        【解决方案5】:
        #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;
        }
        

        【讨论】:

          猜你喜欢
          • 2021-05-01
          • 1970-01-01
          • 2020-07-30
          • 2023-03-07
          • 1970-01-01
          • 1970-01-01
          • 2012-04-05
          • 1970-01-01
          • 2016-03-09
          相关资源
          最近更新 更多