Reputation: 1191
This is my file1 called main.c
#include <stdio.h>
#include <stdlib.h>
#define MONTHS 12
void ChangeDay(void);
int* days;
int main(void)
{
days = (int*) malloc(MONTHS * sizeof(int));
if(days != NULL)
ChangeDay();
else
return 1;
printf("%2d.\n", days[0]);
return 0;
}
The global variable days is declared as a pointer to type int
, and
malloc
is used to allocate space for 12 integers.
This is my file 2 called day.c
int days[];
void ChangeDay(void)
{
days[0] = 31;
}
When the function ChangeDay
is called, decimal value 31 is assigned to the first element of the array days .
This is code output :
root@where:~gcc -m32 -Wall -o day main.c day.c
day.c:1: warning: array ‘days’ assumed to have one element
root@where:~./day Segmentation fault
I will be grateful if you explain me this results .
My questions :
Upvotes: 2
Views: 4691
Reputation: 223992
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.
Upvotes: 5
Reputation: 58291
Reason for segmentation fault:
int days[]; // is errror
void ChangeDay(void)
{
days[0] = 31;
}
declaration this is error int days[];
, you don't give it size and initialized it. So you can't use
day[0] = 31;
-- this is illegal and causes segmentation fault.
To get an error instead of warning compile your code like:
gcc -pedantic -error
then only it will give you an error.
else this code will not give you an error, Its confusing but check this code its working Codepade. and this code will give you an error if you compile it proper flags as I suggested.
Additionally, as you wants to share same day[]
array declare it as suggested by @Eric Postpischil
I am not deleting my answer presently as I want other users to view the linked code @ codepade and tell me whether it should be compile with new compilers? or its a bug in compiler?
Upvotes: 1
Reputation: 2852
You have to declare the array in one c file, and declare the variable as extern in the other c file.
Upvotes: 0