0

I have a question about character pointers and the addresses that it points to. So I have this lines of code

char* name = "Any name";
printf("%s\n", name);
printf("%p\n", name);
name = "Any other name";
printf("%s\n", name);
printf("%p\n", name);

This is the output to those lines of code

Any name
0x427574
Any other name
0x42757d

My question concerns these addresses, I thought that the addresses would be the same since name is a pointer to an address, so, why does the address of this pointer change after I change what was stored in that address from "Any name" to "Any other name"?

1 Answer 1

1

The first line of your code reads:

char* name = "Any name";
  • Here you declare name, which indeed is a character pointer.
  • And you create a string, "Any name" which is an array of characters ending with a null byte. This string will be stored in memory, somewhere on the heap.
  • And finally you assign the address of that string to the variable name.

That is why name points to the address of the string "Any name".

The fourth line of your code reads

name = "Any other name";
  • Here you create a string "Any other name". This string will also be stored in memory, somewhere else on the heap.
  • and you assign the address of the new string to name.

So you assigned a new value (address) to the variable name.

The end result will look like this: enter image description here

You can see a live version of this visualization at C Tutor; After waiting a couple of seconds, you can step through your code by clicking on the Forward > button. (I added one variable to make the visualization more clear)


Your question was:

I thought that the addresses would be the same since name is a pointer to an address, so, why does the address of this pointer change after I change what was stored in that address from "Any name" to "Any other name"?

Well, you didn't change what was stored in that address. The first string still reads "Any name". But your pointer does not point to it anymore.

And the address of the pointer did not change. Remember that name is a pointer to characters. The address of the pointer is the address of name, which has not changed at all. This address still points to the same piece of memory, in which an address could be stored. After the first line, it contained 0x427574, which is the address of "Any name". And after the fourth line, it contained 0x42757d, which is the address of "Any other name".

So you didn't change the address of name, you changed the contents of name.

And you didn't change the first array-of-characters, you created a second array-of-characters.


And to quote Cliff B:

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

5
  • One addition to clarify just a bit. Peter explained most of it well, but maybe not quite clear about this part. Take the command char* name = "Any name";. This code will create the string "Any name" and store it in memory. As a separate process, it will take the address where the string is stored and put that address in the variable name. Later, there's this: name = "Any other name";. It'll do the same process, storing the new string at yet a different place, and storing the address of the new string in name. Both strings continue to exist, but the address of the first one is now lost.
    – Cliff B
    Oct 7, 2018 at 6:09
  • Hmmm.... do you know what a memory leak is yet? ;-)
    – Cliff B
    Oct 7, 2018 at 6:09
  • Good point! I'll try to edit my answer later today. I didn't include a link to (the C version of) PythonTutor because it was not quite clear enough IMHO. Oct 7, 2018 at 6:19
  • I know what a memory leak is, but I didn't want to talk about garbage collection in this answer. Oct 7, 2018 at 6:20
  • 1
    That was for Luis. ;-)
    – Cliff B
    Oct 7, 2018 at 6:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .