我有c语言的问题,请你解决它.... [英] i have a problem in c language can you solve it please....

查看:56
本文介绍了我有c语言的问题,请你解决它....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从代码中删除了hello消息,那么代码就会以完全相同的方式运行。

if i remove the hello message from the code then the code runs in the exact way.

#include<stdio.h>
int *test();
void main()
 {
   int *a;
   a=test();
   printf("hello");// if i remove this line then the code gives the correct answer what is the reason
   printf("value of a is : ",*a);
   getch();
 }
int *test()
{
 int x=10;
 return &x;
}

推荐答案

所有这些示例中的所有类型都不应该是指针。 :-)



请参阅: http:// www .cplusplus.com / reference / cstdio / printf / [ ^ ]。



那么请注意,在函数 test 中你会得到一个指针到一个局部变量并返回它。你可能不明白局部变量是在堆栈上创建的(但你也应该理解堆栈是如何工作的),所以在返回被调用时,这个变量会随着它的上下文(堆栈帧)而消失,所以你指的是什么? />


-SA
None of the types in all this sample should be pointers. :-)

Please see: http://www.cplusplus.com/reference/cstdio/printf/[^].

By the way, note that in the function test you get a pointer to a local variable and return it. You probably don't understand that local variables are created on stack (but you also should understand how stack works), so on return to the called this variable disappears with its context (stack frame), so what are you pointing at?

—SA


这篇关于我有c语言的问题,请你解决它....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆