Pointer passed to function value doesn't change

For some reason when i pass a string pointer to a function such as string *str = new string(""); and pass that string to a handleElement() function e.g. handleElement(str), and i change the value in the function it simply doesn't change when the function exits as though it's passing it by value or something, even though it gives the pointer address.. I've now changed the code to use double pointers and pass the reference of the str pointer and it works but it seems ugly to use double pointers for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//handles when a new element is encountered when parsing and adds it to the
//parse tree
bool ParseBlock::handleElement(char cur, string *curString, int count, bool isOperator)
{
    countNode = new ParseNode(count);
    //keep track of numbers and strings if they exist and insert them
    if(!curString->empty())
    {
        if(isNumber(*curString))
        {
            if(tempNode != 0)
            {
                parseTree->insertRight(node, tempNode);
                countTree->insertRight(curCountNode, tempNodeTwo);
            }
            tempNode = new ParseNode(atoi(curString->c_str()));
            tempNodeTwo = new ParseNode(count);
        }
        else
        {
            if(tempNode != 0)
            {
                parseTree->insertRight(node, tempNode);
                countTree->insertRight(curCountNode, tempNodeTwo);
            }
            tempNode= new ParseNode(*curString);  
            tempNodeTwo = new ParseNode(count); 
        }
    }
    newNode = new ParseNode(cur);
    parseTree->insertLeft(node, newNode);
    countTree->insertLeft(curCountNode, countNode);
    node = newNode;
    curCountNode = countNode;
    //when symbol is not an operator add the stored node to the right
    if(!isOperator && !curString->empty())
    {
        cout<<"Printing tempNode: ";
        print(tempNode);
        cout<<endl;
        cout<<"Printing tempNodeTwo: ";
        print(tempNodeTwo);
        cout<<endl;
        parseTree->insertRight(node, tempNode);
        countTree->insertRight(curCountNode, tempNodeTwo);
        tempNode = 0;
        tempNodeTwo = 0;
    }
    curString = new string("");
    return true;
}
1
2
3
4
5
bool ParseBlock::handleElement(string *curString)
{
    // ...
    curString = new string("");
}

You really need to pass the pointer by reference, i.e.
1
2
3
4
5
bool ParseBlock::handleElement(string** curString)
{
    // ...
    *curString = new string("");
}
or
1
2
3
4
5
bool ParseBlock::handleElement(string*& curString)
{
    // ...
    curString = new string("");
}


Or better yet, just pass the string by reference:
1
2
3
4
5
bool ParseBlock::handleElement(string& curString)
{
    // ...
    curString.clear();
}
Last edited on
it simply doesn't change when the function exits as though it's passing it by value or something, even though it gives the pointer address.

You need to think clearly about what it is you're trying to pass by reference, and what you're changing the value of. By passing in a string * as the argument, you're effectively passing the string itself by reference. But you are passing the pointer to the string by value.

In your function, the thing you are trying to change is not the value of the string, but the value of the pointer to the string. That's the thing you're passing by value, so the change in that pointer won't be reflected in the calling code.

Remember, a pointer is just a variable like any other. It has a value, which can be changed. It's just that that value is a memory address.

All of kbw's solutions will work.
Topic archived. No new replies allowed.