为什么我的指针在我取消引用main之后与我自己的自定义函数中取消引用它之后有不同的值?
这可能是非常简单的事情,但我无法弄清楚.以下是代码:
int *addNumbers(int a, int b);
int main()
{
int no1 = 1;
int no2 = 3;
int no3 = 8;
int *answer = &no3;
answer = addNumbers(no1, no2);
std::cout << answer << std::endl;
std::cout << *answer << std::endl;
/* Why does this second cout line not give the same
answer as the second cout statement
in the addNumbers function? */
}
int *addNumbers(int a, int b)
{
int *resultPntr;
int result = a+b;
resultPntr = &result;
std::cout << resultPntr << std::endl;
std::cout << *resultPntr << std::endl;
return resultPntr;
}
Run Code Online (Sandbox Code Playgroud)
我以为取消引用resultPntr和answer会给出相同的值?
| 归档时间: |
|
| 查看次数: |
75 次 |
| 最近记录: |