当存在同名的局部变量时访问全局变量 [英] Accessing a global variable when there is a local variable in the same name

查看:93
本文介绍了当存在同名的局部变量时访问全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int i = 10;

int main()

{

int i = 20;

返回0;

}


大家好,


我想在主要内部访问全局变量i 。有没有

a在C中这样做的方法?


问候,

Mohan。

int i = 10;
int main()
{
int i = 20;
return 0;
}

Hi All,

I want to access the global variable i inside the main. Is there
a way to do this in C?

Regards,
Mohan.

推荐答案

Mohanasundaram写道:
Mohanasundaram wrote:
int i = 10;
int main()
{
int i = 20;
返回0;
}
大家好,

我想访问main中的全局变量i。是否有一种方法可以在C中执行此操作?
int i = 10;
int main()
{
int i = 20;
return 0;
}

Hi All,

I want to access the global variable i inside the main. Is there
a way to do this in C?




不通过在main内部重新声明来屏蔽它。



By not masking it with a redeclaration inside main.


2004-06-25,Mohanasundaram< mo ************ @ msn.com>写道:
On 2004-06-25, Mohanasundaram <mo************@msn.com> wrote:
int i = 10;
int main()
{
int i = 20;
返回0;
}

我想在main中访问全局变量i。有没有在C中做到这一点的方法?
int i = 10;
int main()
{
int i = 20;
return 0;
}

I want to access the global variable i inside the main. Is there
a way to do this in C?




int i = 10;

int main(void)

{

int * global_i_pointer =& i;

int i = 20;


/ * ...使用global_i_pointer从主内部访问全局变量i

... * /


返回0;

}


- James



int i = 10;
int main(void)
{
int *global_i_pointer = &i;
int i = 20;

/* ... use global_i_pointer to access the global variable i
from inside main ... */

return 0;
}

-- James


Mohanasundaram写道:
Mohanasundaram wrote:
int i = 10 ;
int main()
{
int i = 20;
返回0;
}

大家好,

我想访问main中的全局变量i。有没有在C中做到这一点的方法?
int i = 10;
int main()
{
int i = 20;
return 0;
}

Hi All,

I want to access the global variable i inside the main. Is there
a way to do this in C?




有简单的方法和聪明的方式。


简单的方法是将本地i重命名为其他东西,或者将全局i重命名为
更合理的东西(一个名为i的全局变量

可能是一个非常糟糕的主意。)


smararse方式涉及使用指针或嵌套的extern声明。

我不会告诉你细节。只需重命名我的一个或两个。


-

Chris" electric hedgehog" Dollin

C常见问题解答: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html

C欢迎: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


这篇关于当存在同名的局部变量时访问全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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