-1

I understand that using const char* is a modifiable pointer to a constant character. As such, I can only modify the pointer, but not the character. Because of this, I do not understand why I am allowed to do this:

const char* str{"Hello World"};

str = "I change the pointer and in turns it changes the string, but not really.";

How does this work? Is there somewhere in memory where all the characters are stored and I can just point to them as I wish? Furthermore, the adress of str does not change throughout this process. Since the only thing that can change is the address, I really don't understand what's going on.

Maybe part of the problem is that I try to understand this as if the string was an integer. If I do:

int number{3};
const int* p_number{&number};

*p_number = 4;

This is not valid, hence why I expect str to not by modifiable. In order words, where am I pointing so that "Hello World" becomes "I change the pointer and this changes the string"?

EDIT:

I get that I create a new string at another address, but when I do:

const char *str{"HelloWorld"};
std::cout << &str << std::endl;
str = "I create new string, but I get same address";
std::cout << &str << std::endl;

I always get the same address twice.

  • This is explained in any beginner level [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). In particular, `"I change"` is a string literal and **decays** to `const char*` which is then assigned to `str`. See this: [Question about the type of `&"hello"` and `"hello"`](https://stackoverflow.com/questions/72342528/question-about-the-type-of-hello-and-hello). The same is explained here: [The parameters of the Main function in C++](https://stackoverflow.com/a/71965951/12002570) – Jason Aug 12 '22 at 15:15
  • `str` is not `const`. `p_number` is not `const`. What they *point to* is `const`. – Eljay Aug 12 '22 at 16:09

2 Answers2

0

You don't modify the original string. You are reassigning the pointer to a new string. The character string is the constant, not the pointer that points to it.

To create a const pointer you could const char* const str = "..."; This makes the pointer constant as well.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
0

No string was replaced, you just reassigned str a new string which is stored on stack-memory.

Try this snippet, that the address was changed

#include <stdio.h>

int main(void)
{
    const char *ptr = "Hello";
    printf("Before: %p\n", ptr);

    ptr = "World";
    printf("After: %p\n", ptr);
}

Output [RESULT MAY VARY]:

Before: 0x402004
After: 0x402016
Darth-CodeX
  • 2,166
  • 1
  • 6
  • 23
  • 1
    *"which is stored on stack-memory"* This part is not correct. String literals are not stored on the stack; they have static storage duration and a lifecycle matching that of the entire program. In practice, they are generally stored in the read-only (`.TEXT`) section of the object file. – Cody Gray - on strike Aug 12 '22 at 15:18
  • Your code does give new address in C++, which is what I expected. However when I do: const char *str{"HelloWorld"}; std::cout << &str << std::endl; str = "I create new string, but I get same address"; std::cout << &str << std::endl; I get the same address everytime (different each execution): 0xe8c37ff858 0xe8c37ff858 How is that different than what you did? – Nathaniel Brochu Aug 12 '22 at 15:36
  • 1
    @NathanielBrochu Your code uses the address-of operator `&` to get the address of the pointer variable. The code in this answer prints the values the pointer contains, the addresses of the string literals. The pointer doesn’t move and lives at the same address but _where it points to_ changes. – Blastfurnace Aug 13 '22 at 01:56
  • @Blastfurnace That's a nice explanation! – Darth-CodeX Aug 13 '22 at 07:09
  • Aaaaah, thank you so much @Blastfurnace. That makes much more sense! – Nathaniel Brochu Aug 14 '22 at 20:02