Interview Questions

How can code in a file where an array is declared as extern

C Interview Questions and Answers


(Continued from previous question...)

How can code in a file where an array is declared as extern

Q: How can code in a file where an array is declared as extern (i.e. it is defined, and its size determined, in some other file) determine the size of the array? sizeof doesn't seem to work.

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