Interview Questions

I have an extern array which is defined in one file

C Interview Questions and Answers


(Continued from previous question...)

I have an extern array which is defined in one file

Q: I have an extern array which is defined in one file, and used in another:

file1.c: file2.c:

int array[] = {1, 2, 3}; extern int array[];
Why doesn't sizeof work on array in file2.c?


A: An extern array of unspecified size is an incomplete type; you cannot apply sizeof to it. sizeof operates at compile time, and there is no way for it to learn the size of an array which is defined in another file.

You have three options:
1. Declare a companion variable, containing the size of the array, defined and initialized (with sizeof) in the same source file where the array is defined:
file1.c: file2.c:
int array[] = {1, 2, 3}; extern int array[]; int arraysz = sizeof(array); extern int arraysz;

2. #define a manifest constant for the size so that it can be used consistently in the definition and the extern declaration:
file1.h:

#define ARRAYSZ 3 extern int array[ARRAYSZ];

file1.c: file2.c:

#include "file1.h" #include "file1.h"
int array[ARRAYSZ];


3. Use some sentinel value (typically 0, -1, or NULL) in the array's last element, so that code can determine the end without an explicit size indication:
file1.c: file2.c:
int array[] = {1, 2, 3, -1}; extern int array[];

(Obviously, the choice will depend to some extent on whether the array was already being initialized; if it was, option 2 is poor.)

(Continued on next question...)

Other Interview Questions