我是C的初学者并且正在阅读一本名为“理解和使用C指针”的书,并最终遇到了使用printf函数的问题 .
当使用debug_p时,事实证明指针num_p的地址不同但在调试中精确到num,但是当在main()中尝试printf时它返回正确的值 .
我的问题是,为什么会这样呢?是否有合适的方法在函数内执行printf并具有正确的指针地址?
#include
void debug(int num)
{
printf("Address of num: %i Value: %i\n", &num, num);
}
void debug_p(int *num_p)
{
printf("Address of num_p: %i Value %i\n", &num_p, num_p);
}
int main()
{
int num=11;
int *num_p=#
printf("Address of num: %i Value: %i\n", &num, num);
printf("Address of num_p: %i Value: %i\n\n", &num_p, num_p);
debug(num);
debug_p(num_p);
return 0;
}
main()的输出:
Address of num: 2358860 Value: 11
Address of num_p: 2358848 Value: 2358860
从debug输出,然后是debug_p:
Address of num: 2358816 Value: 11
Address of num_p: 2358816 Value 2358860