Help you learn/review (complex) C variable and function declarations and definitions
(See Harbison and Steele book for an even more formal presentation)
C Variable Declarations and Definitions
Declaration vs. definition
Scope, linkage, and duration
Variables with process duration exist in either the data or bss sections
Global variable that is explicitly initialized goes in data section
Global variable that is not explicitly initialized goes in the bss section, and is initialized to 0
Examples:
int i = 5; /* data section */ int j; /* bss section */ int k = 0; /* data section */
"bss" stands for "block started by symbol"; maybe was descriptive at one time, but now is not
Now we've seen all 6 sections: text, rodata, stack, heap, data, bss
Global variables
Review examples of global variable declarations and definitions
Review examples of combinations of global variable declarations and definitions
Style: Minimize use of global variables; when you use them, try to make them static (and const)
Global variables with external linkage are a maintenance nightmare
(Code that uses global variables is not thread-safe)
Happily, Splint complains about such variables
Local variables
Review examples of local variable declarations and definitions
Note the two distinct meanings of the "static" keyword
Applied to a global variable => the variable has internal linkage
Applied to a local variable => the variable has process duration
And both of those meanings differ from the meaning of "static" in Java!!!
C Function Declarations and Definitions
Note especially static function definitions
Complex Declarations and Definitions
The "right-left rule"
Read the three steps that define the rule; do so quickly; the examples are more helpful
Some examples (if time is short, cover only the boldface examples):
int x[5]; First eliminate the subscript: int x[]; Now do the translation: x x is x[] x is an array of int x[] x is an array of int Now add back the subscript, and refine the description x is an array of 5 ints
int x(int i); First eliminate the parameter(s): int x(); Now do the translation: x x is x() x is a function int x() x is a function returning an int Now add back the parameter(s) and refine the description: x is a function accepting an int and returning an int
int *x(); x x is x() x is a function int *x() x is a function returning a pointer to int
int *x[]; x x is x[] x is an array of *x[] x is an array of pointer to int *x[] x is an array of pointer to int
int (*x)[]; x x is *x x is a pointer (*x)[] x is a pointer to an array int (*x)[] x is a pointer to an array of int
int (*x)(); x x is *x x is a pointer (*x)() x is a pointer to a function int (*x)() x is a pointer to a function returning an int
int (*x())[]; x x is x() x is a function *x() x is a function returning a pointer (*x())[] x is a function returning a pointer to an array int (*x())[] x is a function returning a pointer to an array of int
int (*x())(); x x is x() x is a function *x() x is a function returning a pointer (*x())() x is a function returning a pointer to a function int (*x())() x is a function returning a pointer to a function returning an int
Observation 1: you should know how to understand such complex declarations
Observation 2: you should never write such things!!!
In practice, they're easy to avoid
If unavoidable, use typedefs to simplify
Copyright © 2009 by Robert M. Dondero, Jr.