C的全球指针
我想知道我如何得到答案 以下代码可以用任何人解释..
用C语言。.
#include <stdio.h>
int*p;
void fun(int a, int b)
{
int c;
c = a+b ;
p=&c;
}
int main()
{
fun(2,3);
printf("%d",*p);
return 0;
}
如果指示在全球范围内声明了指针,如何访问返回主函数后折叠的内存
I want to know how I am getting answer for
the below code can anyone pls explain ..
In c language..
#include <stdio.h>
int*p;
void fun(int a, int b)
{
int c;
c = a+b ;
p=&c;
}
int main()
{
fun(2,3);
printf("%d",*p);
return 0;
}
If the pointer is declared globally how it's possible to access the memory that was collapsed after return to the main function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
函数到期后,您没有本地变量
c
。因此,指针p
指向什么?您的代码具有不可预测的行为。该解决方案是分配一些内存,并将其分配给
p
。请注意,在使用p
结束时释放分配的内存是一种最佳实践。You don't have the local variable
c
after the expiration of the function. So the pointerp
is pointing to what? Your code has unpredictable behavior.The solution is allocating some memory and assigning it to
p
. note that it is a best practice to free the allocated memory at the end of usingp
.