same name for type as for an array-variable

obviously it is possible to use same name for type and for variable-name. compiler doesn't mind. however:

delme-nameclash.c: In function 'int main()':
delme-nameclash.c:4:24: error: local external declaration 'name name []' [-fpermissive]
4 | extern struct name name[];
| ^~~~
delme-nameclash.c:1:8: note: does not match previous declaration 'struct name'
1 | struct name {int a,b,c;};
| ^~~~

and you guessed it:

struct name {int a,b,c;};

int main(){
extern struct name name[];
}

as compiler says, -fpermissive will give no errors but merely a warning. however, g++-7.3.0 has no problem compiling it. is this a habit from old times to do such crazy stuff? the keyword "extern" is at fault here, without it and with an explicit array-size it will compile...

so, which behaviour is correct? did g++-7.3.0 break the c++ standard by accepting such a code? or is it a recent addition to the standard? what is the correct way to import a variable that has been named "name" containing an array of type "struct name" as declared in an external header, all outside of my responsibility, in another package or lib?
The fact that GCC issues a diagnostic message for this code (which becomes an "error" in -pedantic-errors mode) is apparently a bug in GCC. The code is perfectly valid.

Clang will accept this code without any diagnostics. Note that Clang is not perfectly flawless here either. A code like this

1
2
3
4
5
6
7
struct name {int a,b,c;};

int main() {
  extern struct name name[];
}

struct name name[42];


triggers an error message from Clang about non-matching declarations. The error message is justified. However, Clang points to the struct name declaration as the conflicting one, while in reality the conflict is between the two array declarations.
Last edited on
Topic archived. No new replies allowed.