grep
grep

Reputation: 4026

C++ append string pointer

I am running into some unexpected string behavior when I append an Iterator to a string. Basically, I have a file that reads something along the lines of:

int x := 10;
print x;

I have a string already that contains the contents of this file, and I am iterating through it and simply removing whitespace right now.

void Lexer::parse()
{
    Pointer = Filestring.begin(); // both private members
    while(Pointer < Filestring.end())
    {
        if(is_whitespace(0)) // 0 indicates current Pointer position
        {
            advance(Pointer, 1);
            continue;
        }

        CurToken.append(&*Pointer); // CurToken is private member string
        advance(Pointer, 1);
    }

    cout << CurToken << endl;
}

By the end of the loop I would expect CurToken to be a string that contains nothing but the characters with all whitespace removed. Instead I get something like the following:

int x := 1 + 2;
print x;



nt x := 1 + 2;
print x;



t x := 1 + 2;
print x;

...

rint x;



int x;



nt x;



t x;



x;



;

I assume the issue is de-referencing the pointer, and if so, how can I append the current pointer position's character? If I do not de-refrence it, I end up with invalid conversion from ‘char’ to ‘const char*’

Note: is_whitespace() I have tested, and works as expected. So that can be ruled out as a possible problem.

Upvotes: 1

Views: 3496

Answers (2)

Mike Seymour
Mike Seymour

Reputation: 254771

CurToken.append(&*Pointer);

Assuming Pointer is a pointer, that's equivalent to

CurToken.append(Pointer);

and will append the entire tail of the string onto CurToken (assuming it points to a C-style zero-terminated string; if there's no terminator then anything might happen). It looks like you just want to append the current character:

CurToken.append(*Pointer);

UPDATE: you say that Pointer is std::string::iterator. In that case, &*Pointer is still a pointer to the character; and append will still interpret it as a pointer to a C-style string. Since the characters in a string aren't necessarily terminated (although in practice they almost certainly will be, especially in a C++11 library), you've got undefined behaviour.

Upvotes: 7

Component 10
Component 10

Reputation: 10507

What are the types of CurToken and Pointer? CurToken looks like a std::string. Is Pointer a std::string*? append has quite a few overloads. One of them is:

string& append ( const string& str );

If you want to hit that one you should get rid of the '&' to give you just the dereferenced pointer:

CurToken.append(*Pointer);

That should work, but remember that it'll append the whole string not just the character. If it doesn't you probably need to figure out which overload of append() it's hitting. If you want the first character only, try:

CurToken.append(1, (*Pointer)[0]); 

Upvotes: 1

Related Questions