Lecture 09: C-Strings

Feb 7, 2024  β”‚  Last updated Feb 6, 2024 by Charlotte Curtis

HTML Slides html β”‚ PDF Slides PDF

Where we left off

  • Passing arrays to functions with and without const
  • Partially filled arrays
  • Sorting arrays
  • Multidimensional arrays

Textbook Chapter 7

int counts[N_LETTERS] = {};
char letter;
cin >> letter;

while (!cin.eof()) {
    if (is_alpha(letter))
        counts[to_index(letter)]++;
    cin >> letter;
}

Today’s topics

Textbook Section 8.1

Multidimensional array passing

This is probably a good place to use a global constant

Processing row by row

Depending on the data, you might want to process one row at a time:

const int MAX_RECORDS = 100;
const int NUM_FIELDS = 5;
int records[MAX_RECORDS][NUM_FIELDS] = {};

for (int row = 0; row < MAX_RECORDS; row++) {
    read_record(records[row], NUM_FIELDS);
}

emoji ND array check-in 1/2

The following function is intended to initialize a 2D array of integers to all -1. What is wrong with it?

  1. Nothing, should work
  2. arr is not passed by reference
  3. A size is needed for the second dimension
  4. The loop control variables are not initialized
  5. rows and cols should be const
void initialize(int arr[][], 
                int rows, 
                int cols) {
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            arr[r][c] = -1;
        }
    }
}

emoji ND array check-in 2/2

What is the output of the following code?

  1. Nothing, compiler error
  2. Nothing, runtime error
  3. Random garbage
  4. The memory address of arr[][0]
  5. 0 0 0
const int ROWS = 3;
const int COLS = 3;
int arr[ROWS][COLS] = {};

cout << arr[][0] << endl;

C-strings, finally!

The null terminator

An array of chars is not a C-string until it has a null terminator

C-string shorthand

Some C-string gotchas

C-string I/O

The getline function

If you enter more than than size - 1 characters, they’ll be left in the buffer!

get vs. getline

C-Strings plus functions

More <cstring> functions

Example: Hello World the complicated way

Python version

hello = "Hello"
world = "World"

message = hello + " " + world + "!"
print(message)

C++ version

char hello[] = "Hello";
char world[] = "World";

char message[32];
strcpy(message, hello);
strcat(message, " "); 
strcat(message, world);
strcat(message, "!");

cout << message << endl;

strcmp behaviour

For the function call strcmp(str1, str2), the return value is:

char fruit[];
cout << "What kind of fruit would you like? ";
cin >> fruit;

if (strcmp("apple", fruit) == 0) {
    cout << "Great choice, you can make pie!" << endl;
}

What about the string class?

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

Coming up next

Textbook Chapter 10



Previous: Lecture 08: More Arrays
Next: Lecture 10: Structures