【发布时间】:2020-02-03 02:21:01
【问题描述】:
char* ptr = "hello";
ptr = "world";
ptr的地址会变吗?
如果我最初设置ptr = "hello",那么我设置ptr = "world"。 "hello"去哪儿了,就消失了?
案例一:
[更改前]
ptr = [h][e][l][l][o]; // address of ptr = 10001;
[修改后]
ptr = [w][o][r][l][d]; // address of ptr still = 10001;
或
案例2:
[更改前]
ptr = [h][e][l][l][o]; // address of ptr = 10001;
[修改后]
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";
【问题讨论】:
-
你也可以print the address with %p 看看,也许。我不是 100% 清楚你在问什么
-
"hello"是存储在可执行文件中的字符串。它不会消失,但您无法再访问它,除非您在其他地方再次访问它ptr= "hello";
标签: c arrays pointers char ip-address