owner sign in

3.8 C structures

We pause to learn about C structures before taking up I/O programming of the target computer. This is necessary because that programming will require an understanding of structures.

C structures are used to group information that belongs together. The quintessential example is the tuple: coordinates that define a point.1 The following example shows some of the syntax:

#include <stdio.h>

int main() {
  struct point {                  // declare point
    double x;
    double y;
  };
  struct point pt1 = {1.2, 4.5};  // initialize instance
  struct point pt2;               // declare instance
  pt2.x = 2 * pt1.x;              // assign to second instance x
  pt2.y = 3

This is a preview. Read the full book in print or ebook — get it from MIT Press (ISBN 9780262548762).

Instructor with access? Instructor login.