1

I wrote a little programm and I can't pass two-dimensional array words[10][max_row_size] to function notify. Please help me if you can.
a part of code attached.

#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

It is an errors
/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]

Dmitry Kryukov
  • 63
  • 1
  • 11

4 Answers4

0

You pass words on its own: char** words is the argument in the function: i.e.

int notify(char** words){...
Bathsheba
  • 220,365
  • 33
  • 331
  • 451
0

I'm guessing you want notify to print just one word, so you need to change notify to

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

But also the way you're calling notify will probably not produce the results you're after.

notify(words[max_row_size]);

Will try to get you the 100th word out of 10. Which will probably cause a crash.

You probably want to place notify last in your while loop and call it like this

notify(words[i]);

Also, if you have more than 10 words in your dictionary, you're in trouble. You might want to try a vector instead of an array (because vectors can grow dynamically).

Simon
  • 5,783
  • 2
  • 25
  • 34
  • yes, crash `cannot convert «char*» to «char**» for argument «1» to «int notify(char**)»` – Dmitry Kryukov May 17 '13 at 09:07
  • two-dimentional array is not a pointer to pointer so this aint gonna work – spiritwolfform May 17 '13 at 09:20
  • No, but when passed to a function you can treat them as if they were. Of course you would have to change the call to notify(words). I had a closer look at the code and tried to suggest some code that does what I think the OP is actually trying to achieve. – Simon May 17 '13 at 09:36
  • I want output random element of words every 10 minutes. I call notify every 10 minutes from main. Please see https://github.com/remasik/notify_words/blob/master/notify_words.cpp Firstly it finds amount of rows in my dictionary in main() to `int strings`, then it creates array `words[strings][max_row_size]`, max_row_size = 100. Then content of dictionary writes to `array words` and then I try to pass array `words` to notify function and output random string. – Dmitry Kryukov May 17 '13 at 12:19
0

Easiest way for 2 dimensional array (obviously, you can typedef your array):

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

Easiest for an array of strings:

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

This way prevents that the array is decayed to a pointer in the function, so the size is still known.

stefaanv
  • 12,981
  • 2
  • 27
  • 48
0
notify(char words[][max_row_size])

to pass the whole array down

then use notify(words); to call the method

But really you should be using standard containers instead of arrays

spiritwolfform
  • 2,153
  • 13
  • 16