Lab 3: Structs and strings

The goal of this lab is to learn the basics about strings and structs in C.

1. Introduction

You are writing software for a Twitter knock-off called Chatter. In the Chatter system, users send messages at most 140 characters long. Additionally, each message has the user id of the user who sent the message (at most 16 characters long), a time stamp indicating the time the message was sent (a long int indicating the number of milliseconds since midnight, Jan 1, 1970), and up to 5 hash codes indicating categories or topics the message falls under (each hash code is at most 10 characters long).

You have been provided some code for producing "random" text components and "random" user names. Your tasks will include designing and implementing a struct to represent messages, writing functions for displaying and sorting messages, and completing drivers to test these messages.

2. Set up

Set up a new directory for this lab and move into it.

mkdir lab3
cd lab3

Copy starter code from the course directory.

cp /cslab/class/csci245/lab3/* .

3. Strings in C

In order to do this lab, you need to know a little more about how strings work in C. Recall that there is not really a string type (in the style of Java class String); all that we have in C are:

  1. a way to write string literals (with which you are already familiar), and
  2. library functions that can often treat an array of char as a string.

To use an array of characters as a string, it has to contain a special character to mark its end; that character has the integer value 0, which as a char would be written '\0'. The historic name for that character is NUL, though it is more commonly spelled null, and so these strings are usually described as null-terminated.

When you write a string literal the array value created for it includes the terminating zero; so, for example, the literal "foo" specifies an array of four characters: 'f', 'o', 'o', '\0'. So when we talk about a string of length 7 (or containing 7 characters), that requires a char array of size 8.

There are library routines that let you do useful things with strings; they are declared in the include file string.h. The ones you might find useful in this lab are

There are a few things that require you to be very careful:

  1. strcpy() does not make any space for the destination. You must have already declared an array that is large enough to hold the one being copied--including the terminating 0. If you try to copy a string to an array that is too small, you will make a mess of your program.
  2. When you are declaring character arrays to hold string values, you must include an extra space for the terminating 0.
  3. The library functions all assume that you pass them properly terminated strings, but when you declare a character array, you get no promise about what is in it. Be sure that you initialize them properly. Wierd things can happen when you call library functions with arrays that are not properly null-terminated.
  4. When you are declaring character arrays to hold string values, you must include an extra space for the terminating 0.
  5. When you are declaring character arrays to hold string values, you must include an extra space for the terminating 0.

Note that because strings are character arrays, you can manipulate the individual characters. That means that you can make the string in a character into a zero-length string (an empty string) by assigning a '\0' to element 0.

Keep in mind that you cannot use the comparison operators (such as > or ==) on string values. Also, remember that strcmp() is not a comparison for equality; if you want to test whether two strings are equal, you have to something like

strcmp(a, b) == 0

You will find more information about strings are in chapter 4 of On to C.

4. Provided code

The chatterUtil library provides the following functions:

5. Tasks

You need to complete the following tasks:

6. Pretty displaying of the time (optional)

If you want to make a readable formatting of the time stamp, do the following things to your code:

Then the display of a message would look something like

-----------------------
Sent by Willis at Thu Sep  8 12:32:37 2011

Q: Why do elephants stomp on people?
A: They like the squishy feeling between their toes.
#surplus
#moisture

7. Handing in

Be sure that your names are in all of the files you modified. Turn in your files with

/cslab/class/csci245/bin/handin lab3 makefile message.h message.c sortDriver.c

Thomas VanDrunen, Cary Gray
Last modified: Thu Jan 31 09:50:38 CST 2013