【问题标题】:Passing a 2D array of strings to a function in c++将二维字符串数组传递给c ++中的函数
【发布时间】:2013-02-27 11:06:33
【问题描述】:

我正在尝试将二维和单维字符串数组传递给函数,但它不起作用。

我的数组是:

    string 2Darray[100][100];
    String 1Darray[100];

现在的功能:

    void check(string temp2D[100][100], string temp1D[100]);

当我调用它时:

    check(2Darray,1Darray);

我已经尝试过其他方式,但它们都不起作用。 提前感谢您的任何回答!

【问题讨论】:

  • 你的 1Darray 在我看来是二维的
  • string 1Darray ,作为一维数组有两组索引,在其声明中??
  • C++ 中不能以数字开头标识符,即2Darray 必须是twoDarray

标签: c++ c string multidimensional-array


【解决方案1】:

您可以更改为接受引用:

void check(string (&temp2D)[100][100], string (&temp1D)[100]);

或指针:

void check(std::string temp2D[][100], std::string temp1D[]){}

与下面的语法相同,只是语法不同:

void check(std::string (*temp2D)[100], std::string* temp1D){}

另外,变量名不能以数字开头,2Darray 等是编译器错误。

这是一个完整的工作示例:

#include <string>

void check(std::string (&temp2D)[100][100], std::string (&temp1D)[100]){}

int main()
{
    std::string twoDarray[100][100];
    std::string oneDarray[100];
    check(twoDarray,oneDarray);
}

【讨论】:

    猜你喜欢
    • 2015-05-27
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2021-05-10
    相关资源
    最近更新 更多