CSC 101 Lecture Notes Week 7 C Structures Reading: Chapter 11 - - PowerPoint PPT Presentation

csc 101 lecture notes week 7 c structures reading chapter
SMART_READER_LITE
LIVE PREVIEW

CSC 101 Lecture Notes Week 7 C Structures Reading: Chapter 11 - - PowerPoint PPT Presentation

CSC101-S10-L7 Slide 1 CSC 101 Lecture Notes Week 7 C Structures Reading: Chapter 11 CSC101-S10-L7 Slide 2 I. Intro to C Structures A. An array has multiple elements of the same type . B. A structure has multiple elements of different types .


slide-1
SLIDE 1

CSC101-S10-L7 Slide 1

CSC 101 Lecture Notes Week 7 C Structures Reading: Chapter 11

slide-2
SLIDE 2

CSC101-S10-L7 Slide 2

  • I. Intro to C Structures
  • A. An array has multiple elements of the same type.
  • B. A structure has multiple elements of

different types.

slide-3
SLIDE 3

CSC101-S10-L7 Slide 3

C Structures, cont’d

  • C. Consider first example in Ch 11 of the book.
  • 1. It’s a structure for planet, with components:
  • name
  • diameter
  • number of moons
  • orbit time
  • rotation time
slide-4
SLIDE 4

CSC101-S10-L7 Slide 4

C Structures, cont’d

  • D. The example code is here:

101/examples/structs/jupiter.c

slide-5
SLIDE 5

CSC101-S10-L7 Slide 5

  • II. Arrays of Structures
  • A. A struct type can be the same as any other C

type, such as int or double or char *.

  • B. Hence, arrays of struct types are just fine.
  • C. For example:
slide-6
SLIDE 6

CSC101-S10-L7 Slide 6

Arrays of Structures, cont’d

#define MAX_PLANETS 100 typedef struct { double diameter; Planet planets[MAX_PLANETS]; char galaxy[STRSIZ]; } SolarSystem;

slide-7
SLIDE 7

CSC101-S10-L7 Slide 7

Defining Types in Header Files

  • A. The upgrade from jupiter.c to
  • ur-solar-system.c was awkward.
  • B. Both programs use type Planet.
  • C. Entire def had to be copied into both files.
  • D. We’d like to hav

e programs share definitions.

slide-8
SLIDE 8

CSC101-S10-L7 Slide 8

Defining Types in Header Files, cont’d

  • E. Solution -- use .h files.
  • 1. They allow definition sharing.
  • 2. Also support clean design of larger programs.
slide-9
SLIDE 9

CSC101-S10-L7 Slide 9

Defining Types in Header Files, cont’d

  • F. Design of Planetary Program:
  • planet.h
  • planet.c
  • planet-test.c
  • solar-system.h
  • solar-system.c
  • solar-system-test.c
slide-10
SLIDE 10

CSC101-S10-L7 Slide 10