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

时间:2014-12-20 11:31:38

标签: c++

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

在下面的示例中,我将如何保留原始值: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... 

1 个答案:

答案 0 :(得分:1)

不,代码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。