There are two source files in my program.
An array is defined in A.cpp.
// compiler: MSVC2005 SP2
// A.cpp
// defines an array of type "int [100]"
int a[100] = {3};
It is used in B.cpp.
// B.cpp
// declares an array of type "int []"
extern int a[];
int main()
{
// prints 3 correctly
cout << a[0] << endl;
return 0;
}
AFAIK, linker will raise an error if it cannot find any matched definition for a declaration if the declared identifier is used. Here, int [] and int [100] are two different types, obviously.
Why, in this case, isn't there any link error? Is it guaranteed by the Standard that array size is trivial during matching of declaration/definition? Or it's just implementation-specific? A quote from the Standard will be appreciated if any.
Edit: iammilind mentioned in his answer that linker can run correctly(his compiler is gcc) even if the type does NOT match between declaration and definition. Is it REQUIRED by the Standard or just a way of gcc? I guess this is a far more important issue to figure out.