Re: declaration shadows a parameter
- From: Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx>
- Date: Tue, 22 Aug 2006 21:52:28 +0200
Richard Tobin schrieb:
In article <lnmz9wwqgb.fsf@xxxxxxxxxxxxxxx>,
Keith Thompson <kst-u@xxxxxxx> wrote:
Because it is NOT an error to override the name of a parameter. It may
be a mistake to do so, but the only who knows that it is a mistake is the one who wrote the code.
As it is legal to override the name the compiler may or may not warn you here.
I don't believe that's correct. Both the parameter and the local
object are at the same block scope, even though the parameter is
declared before the opening '{' of the scope. See C99 6.2.1.
That seems very surprising. It would have broken existing code to
prohibit it in the standard, and as far as I can remember I have never
used a compiler that treated it as an error (rather than a warning).
Have a look at C90, 6.1.2.1, or C99, 6.2.1.
From the latter (#3 and part of #4):
,---
3 A label name is the only kind of identifier that has function scope.
It can be used (in a goto statement) anywhere in the function in which
it appears, and is declared implicitly by its syntactic appearance
(followed by a : and a statement).
4 Every other identifier has scope determined by the placement of its
declaration (in a declarator or type specifier). If the declarator or
type specifier that declares the identifier appears outside of any block
or list of parameters, the identifier has file scope, which terminates
at the end of the translation unit. If the declarator or type specifier
that declares the identifier appears inside a block or within the list
of parameter declarations in a function definition, the identifier has
block scope, which terminates at the end of the associated block.
`---
i.e. you cannot think of function parameters as
foo()
{
/* Out-of-function: parameters and similar */
int bar;
{
/* start of function body */
}
/* take up the value given by return */
int retval = __return;
}
in "pseudo C" but really have to think of it as
foo()
{
/* Out-of-function */
{
/* parameter list */
int bar;
/* start of function body */
}
/* take up the value given by return */
int retval = __return;
}
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
.
- Follow-Ups:
- Re: declaration shadows a parameter
- From: Walter Roberson
- Re: declaration shadows a parameter
- References:
- declaration shadows a parameter
- From: main()
- Re: declaration shadows a parameter
- From: Herbert Rosenau
- Re: declaration shadows a parameter
- From: Keith Thompson
- Re: declaration shadows a parameter
- From: Richard Tobin
- declaration shadows a parameter
- Prev by Date: Re: declaration shadows a parameter
- Next by Date: Re: scanf question using %[^$]
- Previous by thread: Re: declaration shadows a parameter
- Next by thread: Re: declaration shadows a parameter
- Index(es):
Relevant Pages
|