fb

Ads

Pages

Declaration & Definition [C]

Before getting to the bottom of this problem, we need to refresh our memories about some essential C terminology. Recall that objects in C must have exactly one definition, and they may have multiple external declarations. By the way, no C++ mumbo-jumbo here—when we say "object" we mean a C
"thing" known to the linker, like a function or data item.
A definition is the special kind of declaration that creates an object; a declaration indicates a name that allows you to refer to an object created here or elsewhere. Let's review the terminology:


definition => occurs in only one place ---> specifies the type of an object; reserves storage for it; is used to createnew objects
example:

int my_array[100];
 

declaration => can occur multiple times ---> describes the type of an object; is used to refer to objects definedelsewhere (e.g., in another file)
example:

extern int my_array[];
 

The declaration of an external object tells the compiler the type and name of the object, and that memory allocation is done somewhere else. Since you aren't allocating memory for the array at this point, you don't need to provide information on how big it is in total. You do have to provide the size
of all array dimensions except the leftmost one—this gives the compiler enough information to generate indexing code.

0 comments:

Post a Comment