这是代码:
#include <stdio.h>
int *addition(int a, int b);
int result;
int *result_ptr;
int *addition(int a, int b)
{
int c = a + b;
int *d = &c;
return d;
}
int main(void)
{
result = *(addition(1,2));
result_ptr = addition(1,2);
printf("result = %d\n", result); //outputline1
printf("result_ptr = %d\n", *result_ptr); //outputline2
return 0;
}
如果在编写当前代码时对其进行编译和处理,result_ptr 会给出奇怪的值。但是,如果您交换 2 条输出线,则不会。为什么这样做?