C:可以函数返回一个全局变量? [英] C: can a function return a global variable?

查看:113
本文介绍了C:可以函数返回一个全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个我有(在这个意义上的全球,它具有文件作用域)一个全局变量和函数的* .c文件。该函数可以返回变量在其他编译单元中使用的价值?

Suppose a I have a *.c file with a global variable ("global" in the sense that it has file scope) and a function. Can the function return that variable as a value to be used in other translation units?

我认为答案是是。如果不出意外,我想在C回报下的复制语义操作---返回返回前pression的价值。但我不知道。

I assume the answer is "yes." If nothing else, I assume that in C return operates under "copy" semantics---the value of the return expression is returned. But I'm not sure.

推荐答案

是的。而且你是正确的:如果你返回类似的 INT ,那么你会返回其当前的副本。如果返回一个指针,你给他们访问变量本身。

Yes. And you're correct: if you return something like an int, then you'll return a copy of its current. If you return a pointer, you'll give them access to the variable itself.

这篇关于C:可以函数返回一个全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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