2

Apparently I'm a member of a large club that just doesn't understand C-style pointers correctly. Here's my program:

void changethestring (const char * thestring)
{
    thestring = "string two";
}

int _tmain(int argc, _TCHAR* argv[])
{
    const char * teststring = "string one";
    changethestring(teststring);
    printf("The string is: %s.\n", teststring);
    return 0;
}

My intent is that changethestring() should cause the pointer to point to "string two". My logic is that I'm giving the function a pointer. The function should be able to change the pointer to point to a different area in memory. And that change should persist outside the function.

Yet that's not what happens. In the printf() statement, the string is still "string one".

Can anyone explain why this is, what's happening under the covers, and how I can write a function changethestring() to change the string to which the pointer points?

4
  • thestring points to the string. You need to change the pointer itself. Pass a const char ** and change the line to *thestring = "string two" then call with changestring(&teststring)
    – agbinfo
    Oct 27, 2014 at 14:50
  • Here, teststring(having address 0) contains, some memory address, say 200, now you gave that address to some another variable thestring(having address 4) (here, thestring got one copy of what is held by teststring). Now what changed you made at address 4, remained at address 4. Though what you are trying to access in main is actually at address 0. I hope it makes some sense.
    – nIcE cOw
    Oct 27, 2014 at 15:24
  • the code is stating that thestring is constant (I.E. unchangable) so the assignment should fail at compile time. and at execution time the assignment is not being made, because the pointer is defined as constant, so cannot be changed. Oct 27, 2014 at 23:31
  • your only changing the local version of the pointer thestring. You need to change the actual pointer in _tmain() so the call to changethestring(teststring); should be changethestring(&teststring); notice the new '&' so it passes the address of the variable and not the address of the "string one" literal. then this line: thestring = "string two"; should be *thestring = "string two"; Oct 27, 2014 at 23:35

2 Answers 2

1

You should give pointer to a pointer instead of just pointer. The reason for this is that in C, all arguments are sent by value. So in order to change the value of a variable which is a pointer, you need to send a pointer to this variable, therefore, pointer to a pointer.

UPDATE: You can define string literal outside of the changethestring function, but it's not needed, because string literals are stored in a global string table for the whole lifetime of the program. So both this:

const char* STRING_TWO_LITERAL = "string two";

void changethestring (const char ** thestring)
{
    *thestring = STRING_TWO_LITERAL;
}

int _tmain(int argc, _TCHAR* argv[])
{
    const char * teststring = "string one";
    changethestring(&teststring);
    printf("The string is: %s.\n", teststring);
    return 0;
}

and this:

void changethestring (const char ** thestring)
{
    *thestring = "string two";
}

int _tmain(int argc, _TCHAR* argv[])
{
    const char * teststring = "string one";
    changethestring(&teststring);
    printf("The string is: %s.\n", teststring);
    return 0;
}

should work.

6
  • Thanks! I should have been able to figure that out myself. Sometimes it's hard to see the forest for the trees.
    – Nevo
    Oct 27, 2014 at 14:56
  • do you have a reference for that?
    – Ashalynd
    Oct 27, 2014 at 14:58
  • Yes, that is right, checked.
    – Ashalynd
    Oct 27, 2014 at 15:01
  • -1 for the confusion regarding string literals.
    – unwind
    Oct 27, 2014 at 15:02
  • 2
    6.4.5 String literals 5 sequence is then used to initialize an array of static storage duration
    – BLUEPIXY
    Oct 27, 2014 at 15:14
1

By passing the pointer by value, it will get back to its initial value when you exit your function.
You should pass it by reference as Ashalynd suggest or you can also return a new value for this pointer:

const char * changethestring (const char * thestring)
{
     thestring = "string two";
     return thestring;
}

int main(void)
{
    teststring = changethestring(teststring);
}
2
  • How do you expect to change a const char *?
    – ryyker
    Oct 27, 2014 at 15:26
  • The original string is not changed, I return the address of a new one.
    – n0p
    Oct 27, 2014 at 15:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.