繁体   English   中英

c++ 中的索引输入 function 的二维数组

[英]2d array with index input function in c++

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

   #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

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

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

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

所以参数需要声明为

char* (*array)[2]

然后在调用 function 时只需传递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) { ... }

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

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

喜欢:

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 样式数组

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

尝试将“array”重命名为其他单词,例如“table”。 我相信视觉工作室保留了“数组”这个词。

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

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

代码链接相关阅读

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM