Explicitly discuss the lack of bounds-checking in C-based courses to avoid student confusion when using strings and arrays.

  • When working with a 1D array, writing beyond the end of an array may overwrite other variables with unexpected values.
  • When working with strings, consider an example when not enough space is allocated for each string. When one array is stored after another, copying data into the second string may overwrite the null character and cause unexpected results in subsequent printing or processing.
  • When working in a 2D array, students may not realize that working beyond the end of one row may be accessing data at the start of the next row.
    • With a 2D array representing a street grid in a city, where a table entry indicates the presence of a hydrant, a natural question is how far each location is from the nearest hydrant.
    • With a 2D array representing the layout of a store, a table entry might indicate the location of a cash register. A natural question is what is the farthest distance from any point in the store to a cash register.

More about this tip

External Source

Interview with Henry Walker