【发布时间】:2016-04-01 02:09:45
【问题描述】:
以下代码的输出是 2500。它包含指针。有人可以对此给出适当的解释吗?为什么它打印为2500?是通过指针声明还是有其他原因?
#include <stdio.h>
/* Two functions include and they are operated by main function */
int *f(int x) {
/* Creates an variable */
int p;
p = x;
return &p;
}
/* Here the initialization of the function g */
int *g(int x) {
/* Creates an variable */
int y;
y = x;
return &y;
}
/* This creates two pointers called x and y */
int main() {
int *x, *y;
/* Here call the functions f and g */
x = f(100);
/* Here call the function g */
y = g(2500);
/* How does it print 2500? */
/* print the value of x */
printf("%d \n", *x);
return 0;
}
【问题讨论】:
-
@DanLowe 谢谢。为此并修复错字:)
-
编译所有警告和调试信息(例如,
gcc -Wall -Wextra -g,如果使用GCC...)然后使用调试器 (gdb)
标签: c pointers return-value