Re: declaration shadows a parameter



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.
.



Relevant Pages

  • Re: types, variable names and fields
    ... No, assuming the same scope, no two typedefs, objects, or functions ... determine what declaration an identifier refers to. ... For structure/union tags and members, ... declarator or type specifier). ...
    (comp.lang.c)
  • Re: Accessing local variable
    ... char * fun ... "4 Every other identifier has scope determined by the placement of its ... declaration (in a declarator or type specifier). ...
    (comp.lang.c)
  • Re: Portability regarding sizeof() function
    ... Appropriate use of sizeof is essential to writing portable code. ... That seems to define the end of the scope. ... Every other identifier has scope determined by the ... placement of its declaration (in a declarator or type ...
    (comp.lang.c)
  • Re: scratch memory
    ... have function scope. ... "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, ...
    (comp.lang.c)
  • Re: what does this warning mean ?
    ... declarator includes a parameter type list, the list also specifies ... function prototype for later calls to the same function in the same ... If the declarator includes an identifier list,139) ...
    (comp.lang.c)