Lecture 10: Structures

Feb 12, 2024  β”‚  Last updated Feb 11, 2024 by Charlotte Curtis

HTML Slides html β”‚ PDF Slides PDF

Where we left off

  • C-strings: a special kind of array
  • C-string I/O
  • C-string functions
  • Separate compilation

Textbook Section 8.1

const int SIZE = 64;
char sentence[SIZE];

cout << "Enter a sentence: ";
cin.getline(sentence, SIZE);

int i = 0;
int words = 0;
while (sentence[i] != '\0') {
    if (sentence[i] == ' ')
        words++;
}

Today’s topics

Textbook Chapter 10

But first, some getline confusion

More clarification: the const modifier

Separate Compilation

What happens when you run make?

Compiling in multiple steps is annoying, so we dump it in a makefile

# This is "Makefile". Notice that comments begin with "#"
program: lab.o main.o
    g++ main.o lab.o –o program
main.o: main.cpp
    g++ -c main.cpp
lab.o: lab.cpp
    g++ -c lab.cpp

Protecting against multiple #includes

Header guards

#ifndef APPLICANT_H
#define APPLICANT_H

... // contents of applicant.h

#endif // APPLICANT_H

emoji Separate Compilation check-in 1/2

Which of the following are good reasons to use separate compilation? Select all that apply.

  1. It allows us to reuse code in multiple projects
  2. It allows us to separate the main logic from other logical groupings
  3. It prevents duplication of code
  4. It prevents re-compiling code that hasn’t changed
  5. It allows us to use make to compile our code

emoji Separate Compilation check-in 2/2

The #include directive is a preprocessor directive that means:

  1. Check if a header has already been included, then include it
  2. Copy and paste the contents of the header file into the source file
  3. Cross-reference to the associated .cpp file
  4. Compile the header file into an object file

Moving on to structures

Functions with long lists of parameters are painful:

// Calculates the amount owed by the customer based on usage and account limits
void calculate_bill(double base_charge, double usage_limit, double maxMB_used,
                    double endMB, double& over_charge, double& penalty_charge,
                    double& gst_owed, double& total);

// Displays the final bill. If no surcharges are owing, these are not shown.
void print_bill(int account_number, double usage_limit, double beginMB,
                double maxMB_used, double endMB, double base_charge,
                double over_charge, double penalty_charge, double gst_owed,
                double total);

Structure syntax

A field (aka member variable or data member) is a term used to describe a single piece of data associated with a common record

A structure for bill calculations

struct BillInfo {
    int account_num;
    double base_charge;
    double usage_limit;
    double maxMB_used;
    ...
    bool valid;
}

Using your new type

Accessing structure fields

Initializing structure fields

Operations on structures

Structures and functions

Returning structures from functions

emoji Structures vs arrays 1/2

Which of the following is false?

  1. Arrays must contain values of the same type
  2. Structures must contain values of the same type
  3. Arrays are always passed by reference
  4. Structures are passed by value by default
  5. Array elements must be accessed by index position

emoji Structures vs arrays 2/2

What can you infer from the function prototypes shown?

  1. a cannot modify the array
  2. b cannot modify the structure
  3. Both A and B
  4. Neither A nor B
void a(int arr[]);
void b(BillInfo bill);

Structures with array fields

Arrays of structures

Arrays of structures continued

But this is getting a little ridiculous, and probably an indication that your implementation needs work

Coming up next



Previous: Lecture 09: C-Strings
Next: Lecture 11: File I/O and Command Line Arguments