1
char* ptr = "hello";
ptr = "world";

Will the address of the ptr change?

If I originally set ptr = "hello", then I set ptr = "world". Where does "hello" go, it just disappears?

case 1:

[before change]

ptr = [h][e][l][l][o]; // address of ptr = 10001;

[after change]

ptr = [w][o][r][l][d]; // address of ptr still = 10001;

OR

case 2:

[before change]

ptr = [h][e][l][l][o]; // address of ptr = 10001;

[after change]

ptr = [w][o][r][l][d]; // address of ptr still = 10002;
char* ptr = "hello";
ptr = "world";
// maybe 2 minutes later, i change again
ptr = "something else";
Barmar
  • 741,623
  • 53
  • 500
  • 612
Chener Zhang
  • 129
  • 9
  • Related: https://stackoverflow.com/questions/2589949/string-literals-where-do-they-go –  Oct 04 '19 at 18:35
  • You can also just [print the address with %p](https://stackoverflow.com/questions/22250067/how-to-get-address-of-a-pointer-in-c-c) and see, maybe. I'm not 100% clear what you're asking –  Oct 04 '19 at 18:36
  • `"hello"` is a string liteal stored in the executable. It will not be gone, but you can't access it anymore, unless somewhere else you do again `ptr= "hello";` – Paul Ogilvie Oct 04 '19 at 18:42

3 Answers3

2

The pointer will change. The text "hello" remains in memory, but it is no longer accessible in a valid way.

#include <stdio.h>

int main(void)
{
    const char* ptr = "hello";
    printf("The value of ptr is %p\n", ptr);
    ptr = "world";
    printf("The value of ptr is %p\n", ptr);
}

The address of ptr is 0000000000404000
The address of ptr is 0000000000404020
  • So the text” hello” still remains in computer memory even we close the programming software. It just there, never auto-disappear right? – Chener Zhang Oct 04 '19 at 18:49
  • 1
    @ChenerZhang no it will disappear when your program cleanly exits. – sleeping_dragon Oct 04 '19 at 18:51
  • 1
    Note that what you are printing as "the address of ptr" is not the address of ptr -- its the address *in* ptr (the value of ptr). Confusing word choice. – Chris Dodd Oct 04 '19 at 23:11
0

If you are just using pointers directly, the OS will assign memory for "hello" and "world" at separate places and you will essentially change your pointer to those different addresses.

So in case 2 there will be 3 different locations for "hello", "world" and "something else" and the pointer will just get reassigned to the third location when you do ptr = "something else"

Also see: Why can a string be assigned to a char* pointer, but not to a char[] array?

sleeping_dragon
  • 701
  • 1
  • 10
  • 27
0

Reassigning the pointer from &"hello"[0] to &"world"[0] will clearly change its value. (the &[0] just makes the otherwise implicit array decay explicit).

String literals are saved in readonly static memory (=they have the lifetime of the program), and "hello" and "world" most definitely cannot occupy the same spot considering their different contents.

Changing the pointer from "hello" to "hello\0world" could leave the pointer value unchanged because then the compiler could merge the two string literals into one (6.4.5p7).

But none of my installed compilers (tcc, gcc, clang) is doing it.

For

#include <stdio.h>
int main()
{
    char *p;
    p = "hello";
    printf("%p\n", p);
    p = "hello\0world";
    printf("%p\n", p);
    p = "world";
    printf("%p\n", p);
}

I'm getting different pointer values, e.g.:

0x55c30f718004
0x55c30f718014
0x55c30f71800e
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142