局部变量的作用域可用于其它函数的调用 这句话是否是对的,为什么?

请解答下原因,我不太懂这一块。

这句话不准确,如果在main函数里面定义的变量在main函数的作用范围里可以传递给其他函数,如果在除主函数的其他函数的里的局部变量会随着函数的结束而释放空间,所以不能再供别的函数传递参数。而main函数的局部变量时程序运行完后才释放空间的,程序运行完钱可以传递给其他函数
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-07-20
不对,作用域怎么能调用呢?作用域是一个范围,变量的作用域是定义起至函数结尾。该变量只能在本函数内部使用,外部函数不能访问该变量。
第2个回答  2012-07-20
1、一般常用的是在函数体的开头定义的变量为局部变量,作用域就在这个函数体里面({}这两个大括号之间)。其他地方对这个变量不可见(不能使用这个变量),函数退出以后,局部变量被系统收回。
2、局部变量被new了一个内存空间,那么函数退出后,此局部变量仍然还存在。这是new的特性。new了一个内存出来,这个内存必须要用delete才能释放。所以这种情况,即使是局部变量,只要是用new出来的,其他函数也能使用这个局部变量new出来的那个内存,但不一定就是这个变量名了。
第3个回答  2012-07-20
不对
函数内定义的局部变量不能被其他函数直接调用
第4个回答  2012-07-20
Are you in the middle of an exam? Haha just curious.
It really depends on where you declare the variable, heap or stack?

The destructor will always be called for stack local variables, in anther word, a local variable's memory resource is auto-collected after the termination of the according fucntion.

However if you declare something on the heap for example malloc or new, a programmer has to release the memory resource manually otherwise, this will make the memory space accessable to functions outside the local function.
相似回答