Skip to main content
Logo image

Section C.9 C3: Arrays, Structs, I/O

Synopsis.

  • Discuss basic array handling by means of a small example: read in numbers from the console and put them into an array and compute the average/sum/something similar.
  • Understand that arrays degenerate to pointer of first element in the context of R-eval.
  • Emphasize that sizeof is the size of the type and evaluated at compile time. Cannot use it to query size of container at runtime. Makes a difference for malloc'ed containers.
  • Start a more complex example. Read in person records from a file (name, surname, date of birth)
  • Discuss basic I/O printf, fscanf, fopen, fclose.
  • Struct become helpful. Discuss composition vs aggregation. Mention that strings are null-terminated arrays of characters.
  • Simplify program with respect to pointer notation; explain -> and ..
  • Modularize program into different files, creating functions for reading and printing records.
  • Add command line argument for file name and explain argc and argv.

Sections Covered.