CS31 Weekly Lab: Week 11

writing C libraries: .c and .h files and linking, C strings

Week 11 lab topics:

  1. writing C library code, and linking it into programs that use it
  2. .c and .h files
  3. strings in C (and using the readline library)
  4. pointer arithmetic (just FYI)

Create a week11 subdirectory in your weeklylab subdirectory and copy over some files:
    cd cs31/weeklylab		
    pwd
    mkdir week11
    ls
    cd week11
    pwd
    cp ~newhall/public/cs31/week11/* .
    ls

Writing a C library
We are going to learn how to write a C library and how to use it in a program.
First, let's look at my page on Libraries in C. We are going to look at the "CREATING AND USING YOUR OWN LIBRARY CODE" part of this and then look at some example C library code in mylib.[ch] and an example program that uses it (prog.c).

Strings and chars in C
Let's look at the file str.c. This code contains some examples of manipulating strings (and individual chars in a string) in C.

In C, a string is an array of char with a special terminating null character '\0' that signifies the end of the string. The array of chars can be statically or dynamically allocated (by calling malloc). One thing to remember is to allocate enough space for the terminating null character.

Let's take a look at this code and see what it is doing. Note its uses the ctype and string library functions. C string library functions assume that the caller has allocated space for the result string (note the call to strcpy).

example string library functions: 
  strcmp, strlen, strcpy, strchr, 
example ctype library functions:
  isalnum, isdigit, isspace 

This code also shows an example of using the readline library to read in a string entered by the user. I have some documentation about the readline library here: using the C readline library. The call to readline returns a string (allocated in heap space) to the caller containing the contents of the input line. It is the caller's responsibility to free this returned string.

See my C documentation: char in C, strings in C and Part 1 of intro to C for CS31 students for more information. Also, look at man pages for ctype and string library functions to learn more about how to use them.

Try out

  1. In another window, compile and run this program to see what it is doing. Note when it is manipulating the individual char in the string (array of chars) and when it is treating it as a string (using strcpy, strlen).

    Try running with some different input strings, for example:

    hello 1 2 3
        hello   1   2   3 
    !@  hello  x%
    

  2. Next, try implementing code to create a substring for the first token in the str string:
    1. search for the start of the first token (the first non-space char)
    2. continue processing chars until find end of first token (the first space char following sequence of non-spaces)
    3. change the char at the end of the first token to '\0'

    Make the substr pointer point to the begining of the first token and try prining it out. One way to set a pointer to point to any bucket in an array is:

    ptr = &(array[i]);
    

    Compile and try running with different input strings and see if it works.



Pointer Arithmetic in C
We are not going to talk about pointer arithmetic in lab, but if you are interested, you can learn a little bit about it through this example. Pointer arithmetic is never necessary to use to solve a problem, and it can often lead to weird bugs, but in some cases it results in a more consise way to write C code that manipulates pointers and arrays.

Let's look at the file ptrarth.c. This code contains some examples of using a pointer to iterate over arrays of char and arrays of ints. It uses pointer arithmetic to adjust the pointer at each step to point to the next bucket. Adding a value (lets say 3) to a pointer, does not add three to the address value stored in the pointer variable. Instead, it adjusts the pointer the pointer variable to have the address of the the valid storage location of the type it points to that is three locations beyond it. For example:

int array[10], *ptr, *start, num;

ptr = array;
start = ptr;

*ptr = 6;  // puts 6 in bucket 0 of the array
ptr = ptr + 3;  // make ptr point to bucket 3 of the array 
                // which is at address 12 beyond the current value of ptr
*ptr = 8;  // puts 8 in bucket 3 of the array

num = ptr - start;  // 
In the example code, using pointer arithmetic is not necessary (the same functionality can be accomplished without using pointer arithmetic), but it is handy to use in some cases. Try out the TODO item in this file and run and test it.