开发者

Pointer - inquiry

#include <stdio.h>
#include <stdlib.h>

int main( void ) {
   char *ptr1 = "Hello World\n";
   char *ptr2;

   ptr2 = ptr1 + 6;
   ptr2 = "Test!\n";

   printf("%s",ptr2开发者_如何学JAVA);
   printf("%s",ptr1);

   return EXIT_SUCCESS;   
}

Output:

Test!
Hello World

Why didn't I get Hello Test!? I thought id would overwrite the World-part from ptr1.


The line

ptr2 = "Test!\n";

Does not change the contents of the buffer pointed at by ptr2. Instead, it just changes what ptr2 points at. In C, pointer assignment just says "I want this pointer to stop referencing what it used to reference, and to instead point at something else." If you actually want to change the value being pointed at, you either need to dereference the pointer or call a function that dereferences it (for example, strcpy, strcat, etc.)

Consequently, the output is what you've listed above, with "Hello World" intact.


When you assign something to a pointer, such as ptr1 or ptr2. you are not changing the value of what is stored there, you are just changing what they point to.

When you say:

ptr2 = ptr1 + 6;

You are making ptr2 point to the 6th element of ptr1's string. Then you say:

ptr2 = "Test!\n";

This means ptr2 is now pointing to a new, different string, elsewhere in memory, which contains "Test!\n". So you have this:

ptr  ------->  "Hello World\n"
ptr2 ------->  "Test\n"

Now, when you print them, you get:

Test!
Hello World


ptr2 = "Test!\n"

Simply sets ptr2 to point to a new character string. Logically, you meant to write *ptr2 = ...., but now you would be modifying a string literal in read only memory. You would need to declare ptr1 as a char[] so that the string literal is allocated in stack memory if you want to modify it.


Pointers just point to memory addresses.

Your assignments aren't writing anything, they are just changing where ptr2 is pointing.

Also note, you can't go overwriting the constant strings in your executable anyway, it's liable to crash your program.


Because:

   char *ptr1 = "Hello World\n";
   char *ptr2;

At this point you have two pointers, one pointing to a memory location containig "Hello World" and the other uninitialised,

   ptr2 = ptr1 + 6;

Now ptr2 points to a memory location 6 bytes after the location pointed to by ptr1, in other the 'W' of "Hello World". If you outputted ptr2 now you'd see "World".

   ptr2 = "Test!\n";

Now ptr2 points to an entirely different memory location that contains "Test!"

This is why when you output your pointers you see what you do.


Because:

   ptr2 = "Test!\n";

Assigns the address of the constant literal "Test!\n" to ptr2 overwriting the address you assigned on the previous line.


ptr2 = "Test!\n"; Now ptr2 is pointing to the string literal "Test!\n". The string literal content doesn't get modified anywhere .

ptr1 is still pointing to "Hello World\n".

I thought id would overwrite the World-part from ptr1.

And why would you ever think of doing that? Trying to modify the content of a string literal is undefined behaviour.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜