tags:

views:

103

answers:

3

Hi all, let's say I've

foo.c

struct foo {
  int a;
};

bar.c

struct foo {
   char *s; 
   double x,y;
};

The struct definitions are only in .c files. Is it legal according to C standard? Which part of standard says so? Edit: There're no #inclusion of struct definition.

Thank you all for fast response! :D

+1  A: 

If they do not "know" of each other (i.e. via #include or something), that should be no problem. If they do, you might have a look at http://stackoverflow.com/questions/2680502/how-to-resolve-two-structures-with-the-same-name.

phimuemue
+6  A: 

Section 6.2.1-4 of the C99 standard indicates that it is legal as both are declared in different scopes (each having file scope extending from their definition to the end of the translation unit, i.e. the file).

sje397
But linkage says that two identifiers from different scopes can be made to refer to the same object. Wouldn't they both have external linkage in this case?
Owen S.
I read 6.2.2-6 to state that by default, structs have no linkage (a struct not being an object)
sje397
@Owen S: In C language the notion of *linkage* does not apply to *types*. In C linkage applies only to objects or functions.
AndreyT
@AndreyT - to get really pedantic, they have a type of linkage called 'none' :)
sje397
Got it - linkages apply to objects and functions but not types. Thanks!
Owen S.
+6  A: 

The code is perfectly legal C. You might run into problems with debuggers (mistaking one type for another and attempting to display one as another), but it is fine from the language point of view.

There's no part of the standard that would directly say that this is legal. Rather, there's no part of the standard that says it is illegal.

Something like this would be illegal in C++, since C++ extends the concept of linkage to classes and non-local in C++ classes always have external linkage.

AndreyT
Not sure about that second paragraph. There's nothing in there saying I can't put elephants in my code either ;)
sje397
@sje397: Yes, but the standard does not even introduce the concept of "elephant". As for the struct types - they exist in the standard, and the declaration rules for structs are also described in the standard. The standard says what need to be done in order to use struct types in the program, and as long as these requirements are met, you are OK.
AndreyT
Basically, it is like asking if it is legal to name your struct type as `Abcdef`. How would you answer such a question? If you just say "yes", then the next question will be about `Dgefht` as a struct type name. Is this one OK? And so on... So, when it comes to issues like that, the correct answer is that if the standard does not prohibit the identifier like that, then it is OK to use it. Same thing with the struct declaration in the original question.
AndreyT
I disagree. The standard describes what legal names you can use. You would say 'yes' and refer to the standard which describes the rules for struct names. The standard has to say what *is* permitted, because stating what is *not* permitted would be impossible. And in this case, it does say that it is legal.
sje397
@sje397: But that's exactly what I'm talking about. The standard defines the declaration rules for types (struct types included), which are naturally formulated to work within a single translation unit (file scope). And it doesn't say anywhere that it is *not* permitted to reuse the same name for a different type in a different translation unit (a different file scope), specifically because trying to enumerate everything that is *not* permitted would be counterproductive and/or impossible.
AndreyT
@AndreyT: Yes, it's the distinction between 'not saying it is illegal' and 'saying it is legal' that I'm talking about. That's why I mentioned elephants. I think your second paragraph is wrong: the standard does say it is legal, because that's what the standard has to do, because there are so many things that are illegal. The idea that 'no part of the standard says it is illegal' is pretty useless.
sje397