c++ - 将二维字符串数组传递给 C++ 中的函数

标签 c++ c string multidimensional-array

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

我的数组是:

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

现在的功能:

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

当我调用它时:

    check(2Darray,1Darray);

我试过其他方法,但都不行。 提前感谢任何答案!

最佳答案

您可以更改为接受引用:

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

关于c++ - 将二维字符串数组传递给 C++ 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15352703/

相关文章:

c++11返回值优化还是搬家?

c++ - 不知所措...无法弄清楚为什么 XCode 6.1 无法编译我的 C++ 程序

c++ - 通过非静态方法访问静态成员

c++ - 解决 sphinx 中此问题的想法

更改 GtkLabel 前景色

php - 在 PHP 中将数组的第一个元素转换为字符串

C++ 在嵌入式系统中需要汇编

c - 初学者,带源代码的 C 语言 switch 中的字符串。可能的?

python - 分解字符串列并计算字符频率

c - 为什么会出现 'invalid or unsupported image format'错误?