声明阴影参数 [英] declaration shadows a parameter
问题描述
大家好,
当我编译以下代码时,
#include< stdio.h>
void fun(int val)
{
int val; / *问题在这里* /
printf("%d \ nn",val);
}
int main()
{
int num = 1;
fun(num);
返回0;
}
编译器发出警告(为什么不出错?),
函数`fun'':
警告:'val''声明阴影一个参数
但我所期待的是这样的错误,
错误:重新声明`val''
有人可以解释我的警告吗?
感谢您的时间。
Yugi >
不要对上帝说你的问题有多大
说你的问题你的神有多大
main()写道:
大家好,
什么时候我编译下面的代码,
#include< stdio.h>
void fun(int val)
{
int val; / *问题在这里* /
printf("%d \ nn",val);
}
int main()
{
int num = 1;
fun(num);
返回0;
}
编译器发出警告(为什么不出错?),
函数`fun'':
警告:'val''声明阴影一个参数
但我所期待的是这样的错误,
错误:重新声明'val''
有人可以解释我的警告吗?
为了便于参考,我将val-A的参数调用
到函数fun()和val-B变量声明里面
fun()。
警告意味着当val-B在范围内时,
每次你使用val你会得到存储在里面的值
val-B不是val-A中传递的值。这是一个例子:
#include< stdio.h>
int main(void){
int i = 1;
printf("%d \ n",i);
{
int i = 2;
printf("%d \ n",i);
}
printf("%d \ n" ;,i);
返回0;
}
以上代码将产生输出
1
2
1
使用相同的
名称将另一个变量隐藏变量在某些情况下可能会有用,但有些人会认为这种风格很差。请考虑以下
示例。
#define macrofoo(a){\
int i; \
/ *代码,其中使用其他东西i \
*作为索引变量* / \
}
int foo(无效){
int i,a;
/ *更多代码
.. ....
* /
for(i = 1; i< 10; i ++)
macrofoo(a)
}
在上面的例子中我定义的变量
$ mac $ b在macrofoo中取值独立值
来自变量i在foo()中定义。如果你对
非常热衷于使用名称i
作为索引变量而你想在多个地方使用macrofoo
那么你可能会写这样的代码来代替
。出于这个原因,当出现遮蔽
时,你会想要一个警告,而不是
一个错误,这会使代码无法编译。
>
最后,我注意到阴影的概念在其他编程语言中出现了
。
Spiros Bousbouras
在文章< 11 ********************** @ b28g2000cwb.googlegroups .com>
main()< dn **** @ gmail.comwrote:
void fun(int val)
{
int val; / *问题在这里* /
[snippage]
编译器发出警告(为什么不是错误?)...
C标准只要求诊断。一个警告,一个错误,
a蜂鸣声,闪烁屏幕,或从软盘喷出的水
驱动器(如果你还有一个软盘驱动器)都可以是一个 ; diagnostic" ;.
(编译器附带的文档应该说明一些关于它的诊断的内容
。)
参见< http://web.torek.net/torek/c/compiler.html>。
-
In-Real-Life:克里斯托雷克,风河系统
美国犹他州盐湖城(40°39.22''N,111°50.29''W)+1 801 277 2603
电子邮件:忘了它< a rel =nofollowhref =http://web.torek.net/torek/index.html\"target =_ blank> http://web.torek.net/torek/index.html
阅读电子邮件就像在垃圾邮件中搜索食物一样,感谢垃圾邮件发送者。
main()写道:
当我编译下面的代码时,
#include< stdio.h>
void fun(int val )
{
int val; / *问题在这里* /
printf("%d \ n",val);
}
编译器发出警告(为什么不出错?),
gcc错误:
bc:8:错误:''val''重新声明为不同类型的符号
-
Bill Pursell
Hi all,
When i compile following piece of code,
# include <stdio.h>
void fun(int val)
{
int val; /*problem is here*/
printf("%d\n",val);
}
int main()
{
int num = 1;
fun(num);
return 0;
}
Compiler gives a warning (why not an error?),
In function `fun'':
warning: declaration of `val'' shadows a parameter
But what i expected is an error something like this,
error: redeclaration of `val''
Can someone explain me the warning ?
Thanks for your time.
Yugi
Don''t say to GOD how big your problem is
Say to your problem how big your GOD is
main() wrote:
Hi all,
When i compile following piece of code,
# include <stdio.h>
void fun(int val)
{
int val; /*problem is here*/
printf("%d\n",val);
}
int main()
{
int num = 1;
fun(num);
return 0;
}
Compiler gives a warning (why not an error?),
In function `fun'':
warning: declaration of `val'' shadows a parameter
But what i expected is an error something like this,
error: redeclaration of `val''
Can someone explain me the warning ?For ease of reference I will call val-A the parameter passed
to the function fun() and val-B the variable declared inside
fun().
What the warning means is that while val-B is in scope ,
every time you use val you will get the value stored inside
val-B not the value passed in val-A. Here''s an example:
#include <stdio.h>
int main(void) {
int i=1 ;
printf("%d\n",i) ;
{
int i=2 ;
printf("%d\n",i) ;
}
printf("%d\n",i) ;
return 0 ;
}
The above code will produce output
1
2
1
Shadowing a variable by another variable with the same
name can be useful in certain circumstances although
some would consider it poor style. Consider the following
example.
#define macrofoo(a) { \
int i ; \
/* Code which uses among other things i \
* as an index variable */ \
}
int foo(void) {
int i,a ;
/* More code
......
*/
for (i=1;i<10;i++)
macrofoo(a)
}
In the above example the variable i defined
inside macrofoo takes values independently
from the variable i defined inside foo(). If you
are really passionate about using the name i
for index variables and you want to use macrofoo
in more than one places then you might write
such code. For that reason , when shadowing
occurs you''d want to have a warning rather than
an error which would make the code uncompilable.
Finally , I note that the concept of shadowing appears
in other programming languages.
Spiros Bousbouras
In article <11**********************@b28g2000cwb.googlegroups .com>
main() <dn****@gmail.comwrote:void fun(int val)
{
int val; /*problem is here*/[snippage]
Compiler gives a warning (why not an error?) ...The C standards require only a "diagnostic". A warning, an error,
a beep, flashing the screen, or squirting water out of the floppy
drive (if you still have a floppy drive) can all be a "diagnostic".
(The documentation that comes with the compiler should say something
about its diagnostics.)
See also <http://web.torek.net/torek/c/compiler.html>.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
main() wrote:
When i compile following piece of code,
# include <stdio.h>
void fun(int val)
{
int val; /*problem is here*/
printf("%d\n",val);
}
Compiler gives a warning (why not an error?),gcc errors on this:
b.c:8: error: ''val'' redeclared as different kind of symbol
--
Bill Pursell
这篇关于声明阴影参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!