Re: redeclaration of 'p' with no linkage
- From: Peter 'Shaggy' Haywood <phaywood@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 25 Aug 2007 22:38:57 +1000
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!!
.
- References:
- redeclaration of 'p' with no linkage
- From: Ravi
- Re: redeclaration of 'p' with no linkage
- From: Mark McIntyre
- Re: redeclaration of 'p' with no linkage
- From: Kenneth Brody
- Re: redeclaration of 'p' with no linkage
- From: Mark McIntyre
- redeclaration of 'p' with no linkage
- Prev by Date: Re: Are there books about C data structures?
- Next by Date: Problem in C code
- Previous by thread: Re: redeclaration of 'p' with no linkage
- Next by thread: Serializing a glib data type
- Index(es):
Relevant Pages
|