开发者

Array of Pointer and call-by-reference

I have a开发者_StackOverflow little problem with a few simple lines of code.

Following lines I used to call my method:

char** paras = new char*;
inputLength = charUtils::readParameterFromConsole(paras, paraCount, stringBeginningIndex);

The method looks like 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 i = 0; i < 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 the method readParameterFromConsole are the values correct, but in the calling method they aren't correcy any longer. I am facing that problem since I refactored the code and make an new class.

Can anyone give me an advice please?


You are passing back pointers into a stack allocated variable, input when you say inputs[i] = part, because part is a pointer into input handed back by strtok.

http://www.cplusplus.com/reference/clibrary/cstring/strtok/


Your code as 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 i = 0; i < 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 is that you're setting inputs[i] = pointer into local array. That array doesn't exist anymore when the function returns. Undefined behavior if you use any of those pointers.

As I understand it you want an array of "words" as a result.

That's easy to arrange (note: code untouched by 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;
}

// Some call, like
StringVector const words = wordsOf( lineFromUser() );

Again, this is off the cuff code, please just correct any syntax erors.

Cheers & hth.,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜