Re: redeclaration of 'p' with no linkage



Groovy hepcat Mark McIntyre was jivin' in comp.lang.c on Tue, 21 Aug
2007 9:16 am. It's a cool scene! Dig it.

On Mon, 20 Aug 2007 14:31:47 -0400, in comp.lang.c , Kenneth Brody
<kenbrody@xxxxxxxxxxx> wrote:

Mark McIntyre wrote:

Such objects have 'no linkage' and you can't have two of them in the
same scope. See 6.2.2 of the Standard.

Does that mean that this should be valid?

void foo(void)
{
static int *p;
int i;
extern int *p = &i;
}

My compiler doesn't allow it.

Interesting - I think that this ought to be allowed by 6.2.2p4,
though I'm happy to be corrected. Is this a C89 vs C99 thing?

"For an identifier declared with the storage-class specifier extern in
a scope in which a prior declaration of that identifier is visible, if
the prior declaration specifies internal or external linkage, the
linkage of the identifier at the later declaration is the same as the
linkage specified at the prior declaration. If no prior declaration is
visible, or if the prior declaration specifies no linkage, then the
identifier has external linkage."

Identifiers declared within a block and without the extern keyword
have no linkage.

-----------------------------------------------------------------------
6.2.2 Linkages of identifiers
....
2 ... Each declaration of an identifier with no linkage denotes a unique
entity.
....
6 The following identifiers have no linkage: ... a block scope
identifier for an object declared without the storage-class specifier
extern.
-----------------------------------------------------------------------

Therefore, the above extern declaration of p denotes a separate object
with external linkage, clashing with the previous static declaration.
Also, since it has block scope, the extern declaration is not allowed
to have an initialiser; so that's another problem.

-----------------------------------------------------------------------
6.7.8 Initialization
....
Constraints
....
5 If the declaration of an identifier has block scope, and the
identifier has external or internal linkage, the declaration shall have
no initializer for the identifier.
-----------------------------------------------------------------------

--
Dig the sig!

----------- Peter 'Shaggy' Haywood ------------
Ain't I'm a dawg!!
.



Relevant Pages

  • Re: extern decleration intialization
    ... >"extern" is a reference to a variable with static storage class ... A static-duration object -- a variable, or the contents of a string, ... into scope until partway through the declaration. ... The "linkage" of an identifier has to do with whether the identifier ...
    (comp.lang.c)
  • Re: Shared Objects Libraries and Symbols...
    ... ~ 1 An identifier declared in different scopes or in the same scope more ... There are three kinds of linkage: ... each declaration of an identifier with internal ... ~ 4 For an identifier declared with the storage-class specifier extern ...
    (comp.os.linux.development.apps)
  • Re: Clarification required for linkage.
    ... According to me int g;is the tentative definition of g and it has ... linkage as if the "extern" keyword had been present. ... If the declaration of an identifier for a function has no storage- ...
    (comp.lang.c)
  • Is the following undefined behavior?
    ... static int i; ... extern int i; ... declaration is visible with linkage, ...
    (comp.std.c)
  • Re: Can a static function declaration conflict with a non-static declaration?
    ... For an identifier declared with the storage-class specifier ... identifier is visible,23) if the prior declaration specifies ... internal or external linkage, ... "extern" keyword. ...
    (comp.lang.c)