CSC 101 Lecture Notes Week 7
Introduction to C Structures;
Introduction to "Programming in the Large"


Relevant Reading: Chapters 11, 13




  1. The Basic Idea of a C Structure

    1. An array has multiple elements of the same type.

    2. A structure has multiple elements of different types.

    3. Consider the first example in Ch 11 of the book.

      1. It's a structure for planet, with these components:
        • name
        • diameter
        • number of moons
        • orbit time
        • rotation time

    4. Here's its definition as a C struct:
      typedef struct {
          char name[STRSIZ];      /* name of the planet */
          double diameter;        /* equatorial diameter in km */
          int moons;              /* number of moons */
          double orbit_time;      /* years to orbit sun once */
          double rotation_time;   /* hours to complete one revolution on axis */
      } Planet;
      

    5. The example code is here:
      
      101/examples/structs/jupiter.c
      
      

    6. Here are some basic observations about this definition:

      1. The keywords typedef struct are used to start the definition of a C structure.

      2. Curly braces enclose the struct definition, in the same way that curly braces enclose other things in C, like function definitions, loop bodies, and parts of if-else statements.

      3. The struct definition ends with a closing curly brace, followed by the name of the struct.

      4. Each line within the struct defines a field of the structure.

        1. The syntax of a field declaration is exactly the same as a variable or parameter declaration.

        2. Namely, the field has a type and a name.

        3. For example, the field declaration
          double diameter
          
          defines the type of the field as double, with the name diameter.

  2. Arrays of Structures

    1. A struct type can be used in the same way as any other C type, such as int or double or char *.

    2. Hence, arrays of struct types are just fine.

    3. Here's an example of this from page 568 of the book:
      typedef struct {
          double diameter;        /* diameter of system in km */
          Planet planets[9];      /* array of planets */
          char galaxy[STRSIZ];    /* name of the system */
      } SolarSystem;
      

    4. The complete code is here:
      
      101/examples/structs/our-solar-system.c
      
      

    5. Here are some basic observations about this definition:

      1. Using the struct name Planet in the array definition is just like using any other type name.

      2. In this case, the array named planets has 9 elements of the struct type Planet.

  3. Defining Types in Header Files

    1. In the preceding to struct examples, both programs use type Planet.

      1. The definition originated in planet.c.

      2. To define solar-system.c, we manually copied the Planet definition from one .c file into another.

      3. This is type of copying is inconvenient and potentially error prone, particular as programs get larger

      4. What we'd like to do is have programs share type definitions.

    2. The solution to this problem is to use .h files.

      1. They allow definition sharing.

      2. They also support clean design of larger programs, which is what the book calls "programming in the large" in Chapter 13.


  4. The Design of a General Planetary Program as a Collection of .c and .h Files.

    1. Here are the files:
      • planet.h -- the type definition for Planet plus prototypes for functions that operate on planets
      • planet.c -- the implementation of the functions declared in planet.h
      • planet-test.c -- a testing program that has a main function that calls the functions defined in planet.c

      • solar-system.h -- the type definition for SolarSystem plus prototypes for functions that operate on solar systems
      • solar-system.c -- the implementation of the functions declared in solar-system.h
      • solar-system-test.c -- a testing program that has a main function that calls the functions defined in solar-system.c and planet.c

    2. We will go over some details of these files during lecture in class.


  5. Another Example Using Arrays of Structs

    1. In chapter 11 the book talks about databases of information.

    2. A simple version of a database can be defined as an array of structs.

    3. The example in student-info.h and student-info.c is a simple database of student information records.

    4. We'll discuss some of its features during lecture, this week and next.




index | lectures | handouts | programs | labs | examples | solutions | grades