Diomidis Spinellis ha scritto:
fctk wrote: 2) "If, within a translation unit, the same identifier appears with
both internal and external linkage, the behavior is undefined."
i can't imagine an example of an identifier having both internal and
external linkage...
This is not allowed:
static int bar;
int bar;
i can't understand why there is a problem with:
static int bar;
int bar;
let me expand the previous example to something as:
static int bar; /* first declaration of `bar' */
void f1(void) {
bar;
}
int bar; /* second declaration of `bar' */
void f2(void) {
bar;
}
first of all let's highlight the scopes of the two declarations of
`bar'; i will put an /* 1 */ in front of all lines that belongs to the
scope of the first declaration, and an /* 2 */ in front of all lines
that belong to the scope of the second declaration:
static int bar; /* first declaration of `bar' */
/* 1 */
/* 1 */ void f1(void) {
/* 1 */ bar;
/* 1 */ }
/* 1 */
int bar; /* second declaration of `bar' */
/* 2 */
/* 2 */ void f2(void) {
/* 2 */ bar;
/* 2 */ }
both declarations of `bar' have file-scope, becouse they are put outside
of any block and outside of any list of parameter declarations in a
function definition.
please note that, as far as i know, given a certain identifier `foo',
the scope of a declaration of `foo' and the scope of another declaration
of `foo' *can't* overlap. overlapping is possible only for the scopes of
*different* identifiers declarations.
the relevant rules here for the linkage are:
1) If the declaration of an identifier for an object or a function has
file scope and contains the storage-class specifier static, the
identifier has internal linkage.
2) If the declaration of an identifier for an object has file scope and
no storage-class specifier, its linkage is external.
so for rule 1) all instances of identifier `bar' within the scope of the
first declaration of `bar' have internal-linkage, while for rule 2) all
instances of identifier `bar' within the scope of the second declaration
of `bar' have external-linkage:
static int bar; /* first declaration of `bar' */
/* 1 */
/* 1 */ void f1(void) {
/* 1 */ bar; /* this instance has internal-linkage */
/* 1 */ }
/* 1 */
int bar; /* second declaration of `bar' */
/* 2 */
/* 2 */ void f2(void) {
/* 2 */ bar; /* this instance has external-linkage */
/* 2 */ }
so as far as i know there is no instance of some identifier with *both*
internal-linkage and external-linkage.
of course my reasoning is wrong in some points, but i can't understand
where.
sorry for the long message.