c++ - Array of Pointer and call-by-reference -


i have little problem few simple lines of code.
following lines used call method:

char** paras = new char*; inputlength = charutils::readparameterfromconsole(paras, paracount, stringbeginningindex); 

the method looks following:

int charutils::readparameterfromconsole(char** &inputs, int &paracount, int &stringbeginningindex) {     char input[buffer_string_length];      cin.getline(input, buffer_string_length);      if(strlen(input) > 0)     {         bool stringbeginning = false;         char* part = "";         string partstring = "";          for(int = 0; < paracount; i++)         {             if (i == 0)                 part = strtok(input, " ");             else                 part = strtok(null, " ");              inputs[i] = part;         }     } else     {         cout << "error! no input!" << endl;     }      cout << &inputs[0] << endl;     cout << inputs[0] << endl;      return strlen(input); } 

in method readparameterfromconsole values correct, in calling method aren't correcy longer. facing problem since refactored code , make new class.

can give me advice please?

your code i'm writing this:

int charutils::readparameterfromconsole(char** &inputs, int &paracount, int &stringbeginningindex) {     char input[buffer_string_length];      cin.getline(input, buffer_string_length);      if(strlen(input) > 0)     {         bool stringbeginning = false;         char* part = "";         string partstring = "";          for(int = 0; < paracount; i++)         {             if (i == 0)                 part = strtok(input, " ");             else                 part = strtok(null, " ");              inputs[i] = part;         }     } else     {         cout << "error! no input!" << endl;     }      cout << &inputs[0] << endl;     cout << inputs[0] << endl;      return strlen(input); } 

a main problem you're setting inputs[i] = pointer local array. array doesn't exist anymore when function returns. undefined behavior if use of pointers.

as understand want array of "words" result.

that's easy arrange (note: code untouched compiler's hands):

#include <vector> #include <string> #include <sstream> #include <stdexcept>  bool throwx( char const s[] ) { throw std::runtime_error( s ); }  typedef std::vector<std::string>  stringvector;  std::string linefromuser() {     std::string line;     std::getline( cin, line )         || throwx( "linefromuser failed: std::getline failed" );     return line; }  void getwordsof( std::string const& s, stringvector& result ) {     std::istringstream stream( s );     std::string        word;     stringvector       v;      while( stream >> word )     {         v.push_back( word );     }     result.swap( v ); }  stringvector wordsof( std::string const& s ) {     stringvector result;     getwordsof( s, result );     return result; }  // call, stringvector const words = wordsof( linefromuser() ); 

again, off cuff code, please correct syntax erors.

cheers & hth.,


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -