Every declaration of an identifier for an object must have compatible type with the other declarations. int *days
and int days[]
are different types. The former is a pointer to an int
. The latter is an array of int
.
In the first file, use:
int *days;
In the second file, use:
extern int *days;
Additionally: int *days;
is a tentative definition of days
. When the compiler reaches the end of the translation unit (the source file being compiled), it changes the tentative definition into a definition of the object (with a zero initializer). extern int *days;
is a declaration of days
that is not a definition. It tells the compiler that days
is a name for an object that exists somewhere else.
You should have only one definition for each object. Other files that refer to the object should only declare the name and not define the object.
Sometimes there is confusion about a declaration such as int days[]
because using this in a function parameter declares the parameter to be of type int *
. This is a special adjustment that happens only in function parameters and not in other declarations.