【问题标题】:How to pass two-dimensional array to function [duplicate]如何将二维数组传递给函数[重复]
【发布时间】:2013-05-12 09:19:37
【问题描述】:

我写了一个小程序,我无法将二维数组words[10][max_row_size] 传递给函数notify。如果可以,请你帮助我。
附上部分代码。

#include <iostream>
#include <cstdlib> 
#include <fstream> 
#include <string.h>
#include <unistd.h>
using namespace std;
#define max_row_size 100
int notify(char words[max_row_size]);

int main(void) {
    ifstream dictionary("dictionary.txt");
    //ditrionary looks like 
    //hello-world
    //universe-infinity
    //filename-clock
    string s;
    int i=0;
    char words[10][max_row_size];
    while(!dictionary.eof()){
        dictionary>>s;
        strcpy(words[i++],s.c_str());
    }
        notify(words[max_row_size]);

    return 0;
}

int notify(char words[max_row_size]){
        cout<<words[1];
    return 0;
}

It is a full code of my programm, may be it can help you

这是一个错误
/home/rem/projects/github/notify_words/notify_words.cpp: В функции «int notify(int, char*)»:
/home/rem/projects/github/notify_words/notify_words.cpp:65:113: предупреждение: format «%s» expects argument of type «char*», but argument 3 has type «int» [-Wformat]

【问题讨论】:

  • 一般来说,发布代码产生的错误总是好的。这样更容易提供帮助。
  • 这是一个全面的答案:stackoverflow.com/a/8767247/1837457
  • 编译结果-OK
  • 执行错误bash: string 1: 32447 error of segmentation (ready dump core) '/home/rem/projects/development/cpp_test/1'
  • 在完整代码上添加我帖子的链接。

标签: c++ arrays


【解决方案1】:

您自己传递单词:char** words 是函数中的参数:即

int notify(char** words){...

【讨论】:

    【解决方案2】:

    二维数组最简单的方法(显然,你可以 typedef 你的数组):

    int notify(std::array<std::array<char, max_row_size>, 10>& words){
        std::cout << words[1];
        return 0;
    }
    

    最简单的字符串数组:

    int notify(std::array<std::array<std::string>, 10>& words){
        std::cout << words[1];
        return 0;
    }
    

    这样可以防止数组衰减到函数中的一个指针,所以大小仍然是已知的。

    【讨论】:

      【解决方案3】:
      notify(char words[][max_row_size])
      

      将整个数组向下传递

      然后使用notify(words);调用方法

      但实际上你应该使用标准容器而不是数组

      【讨论】:

        【解决方案4】:

        我猜你想让 notify 只打印一个单词,所以你需要将 notify 更改为

        int notify(char* word){
            cout<<word;
            return 0;
        }
        

        但您调用 notify 的方式也可能不会产生您想要的结果。

        notify(words[max_row_size]);
        

        将尝试让您在 10 个单词中获得第 100 个单词。这可能会导致崩溃。

        您可能希望在您的 while 循环中最后放置通知并像这样调用它

        notify(words[i]);
        

        另外,如果您的字典中有超过 10 个单词,您就有麻烦了。您可能想尝试使用 vector 而不是数组(因为向量可以动态增长)。

        【讨论】:

        • 是的,崩溃cannot convert «char*» to «char**» for argument «1» to «int notify(char**)»
        • 二维数组不是指向指针的指针,所以这行不通
        • 不,但是当传递给函数时,您可以将它们视为它们。当然,您必须将调用更改为 notify(words)。我仔细查看了代码,并试图提出一些我认为 OP 实际上试图实现的代码。
        • 我想每 10 分钟输出一次单词的随机元素。我每 10 分钟从 main 打电话通知一次。请参阅github.com/remasik/notify_words/blob/master/notify_words.cpp 首先它在我的字典中找到 main() 到 int strings 的行数,然后它创建数组 words[strings][max_row_size],max_row_size = 100。然后字典的内容写入 array words,然后我尝试通过数组words通知函数并输出随机字符串。
        猜你喜欢
        • 2011-06-13
        • 2013-01-18
        • 2021-07-15
        • 2013-03-28
        • 1970-01-01
        • 2020-11-01
        相关资源
        最近更新 更多