Thinking in C and *nix (CSC 282 2015S) : Outlines

Outline 11: Miscellaneous C Programming Issues


Held: Thursday, 16 April 2015

Back to Outline 10 - Improved Make. On to Outline 12 - Testing.

Summary

We consider a variety of smaller issues that might make you a better C programmer.

Related Pages

Overview

Administrivia

Sorting

/**
 * Sort an array of strings alphabetically.
 */
int strings_sort (int n, char *strings[])
/**
* Sort an array of strings using comparator.
 */
int strings_sort (int n, char *strings[], "*something-that-compares-strings*" compare)

Function Pointers

int strings_sort (int n, char *strings[],
                  int (*compare)(char *str1, char *str2));
int (*binary_integer_function)(int x1, int x2);
typedef int (*StringComparator)(char *str1, char *str2);
int strings_sort (int n, char *strings[], StringComparator compare);
(*compare)(str1, str2)
compare (str1, str2);

assert - simple program checking

  i = 5;
  // I assume i is 5
  foo (i);
  // I assume i is still 5, since C is a pass-by-value language

Memory Issues in C Programs

Valgrind: Tracking memory issues