2

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]

7
  • In general, it's always good to post the error that your code produces. That way it's easier to help. Commented May 17, 2013 at 8:56
  • Here is a comprehensive answer: stackoverflow.com/a/8767247/1837457 Commented May 17, 2013 at 8:57
  • compilation result - OK Commented May 17, 2013 at 9:08
  • error in execute bash: string 1: 32447 error of segmentation (ready dump core) '/home/rem/projects/development/cpp_test/1' Commented May 17, 2013 at 9:09
  • Adding a link to my post on full code. Commented May 17, 2013 at 10:41

4 Answers 4

0

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

int notify(char** words){...
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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).

4 Comments

yes, crash cannot convert «char*» to «char**» for argument «1» to «int notify(char**)»
two-dimentional array is not a pointer to pointer so this aint gonna work
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.
I want output random element of words every 10 minutes. I call notify every 10 minutes from main. Please see 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.