gpt4 book ai didi

c++ - 如何保存指针最初指向的原始值

转载 作者:行者123 更新时间:2023-11-28 06:31:01 25 4
gpt4 key购买 nike

这里是新程序员...任何帮助将不胜感激...

在我下面的示例中,我将如何保留原始值:0x4000000,以便一旦我这样做:(int)(0x4000000) = 900;在我将其更改为 900 或我决定在那里设置的任何值之前,我仍然可以拥有 0x4000000 的原始值....

int apples = *(int*)(0x4000000);   // lets say that after doing this apples is assigned the value of 10(meaning 10 apples)
// now how do i backup this original of value 10...
*(int*)(0x4000000) = 900; // set the apples to 900 and go on...

最佳答案

不,在你编码 apples == 10 和 *(int*)(0x4000000) == 900

之后

如果你做了

int *apples = (int*)(0x4000000);

然后在最后 *apples 将有 900 个。您可以通过以下操作节省 10 个

int apples_save = *apples; .

像这样

int *apples = (int*)(0x4000000);
int apples_save = *apples; .
*(int*)(0x4000000) = 900;

现在 *apples == 900 和 apples_save == 10 。

关于c++ - 如何保存指针最初指向的原始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27579769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com