UMBC CMSC201, Computer Science I, Fall 1994
Sections 0101, 0102 and Honors
Tuesday December 13, 1994
Assigned Reading: 17.1
Handouts (available on-line): none
- We discussed the meaning of the keywords static
and extern in front of a variable declaration.
If a local variable in a function is declared with the storage class
static, then its value is preserved from between
calls to that function, as shown in this
example. Static local variables
are similar to global variables because the variable is preserved
between function calls. However, static local variables can
only be used in the function where it is declared. So, a good
way to think of static local variables is as a private global
variable that can only be used by that function.
Functions can also be given the static designation.
In that case, only the functions implemented in that file can
use the static function. So, static functions are private
functions that cannot be used by functions implemented in
a separate file.
When a global variable is given the extern
storage class, this indicates that the variable is a global
variable declared in a separate file. Changes made to this function
would be seen by functions in both files.
If a global variable is declared with the static storage
class, then it cannot be used by functions in a separate file
using the extern storage class. Again, we can think of
static global variables as private global variable that can only
be used by functions in that file.
Here's an example with two files: file1.c
and file2.c. The variable
file_1_var is a global variable declared in
file1.c. The main function in file2.c
can use this variable by declaring it as an external variable.
There is also a variable called private declared in
file1.c with the storage class static. The
main function in file2.c cannot access this variable
directly. It must use the functions set_private and
print_private to use this static global variable. Using
static global variables gives us better control over how and when
global variables are used. Sample run.
- Next, we talked about structures (or records). Structures
allow us to combine several pieces of data with differing types
under one variable name. In our first
example, we declare a new type called student_record
using the typedef facility. Variables of the type
student_record can hold two strings and a double in
the fields called name, major and gpa.
Sample run. If the variable
rec is of type student record, then the fields accessed
using rec.name, rec.major and rec.gpa.
These fields can then be used just like any other string or
double variable. ANSI C also allows us to pass structures
as parameters, use structures as return values from functions,
and assign one structure to another (older versions of C do not).
- Our second example uses an
array of structures. Sample run.
- Our third example expands
the student record type to include two new fields that point
to other student records. Note that in the type definition
of this new structure, we need to use the dummy tag.
Sample run. Suppose that
student2 is a student record and ptr
is a pointer to student records. Then after the assignment
p = &student2, the name field of student2
can be accessed as either (*p).name or p->name.
Note that in the first case the parentheses are necessary because
the field operator . has higher precedence than the dereference
operator *. Also, what was said in class about the
-> operator was correct.
Finally, we only had time to look at one example of a recursive
function. A recursive function is a function that calls itself.
Each time the function calls itself, it gets a new set of parameters
and local variables. The parameters and local variables from a
previous call to the recursive function will still be preserved
until the function returns from the previous call. Our
example of a recursive function
simply counts down to 0 and returns.
In this example, if countdown called itself with
countdown(k) instead of countdown(k-1),
the function calls would never terminate.
- The students filled out the Student Course Evaluation Questionnaire
- Quiz 3 was handed back.
Last Modified: September 7, 1994
Richard Chang, firstname.lastname@example.org