cgccgcc4

Pointer to array shared between multiple source files


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 :


Solution

  • 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.