The Point of Strings

We'll cover one last connection between pointers and arrays in this lab. That is the representation and manipulation of strings in C. In C, a string is an array of characters where the end of the string is marked by the null character '\0'). Suppose we have a character array called buf with 10 characters. If buf holds the string "Hello" as in the declaration:

char buf[10] = "Hello" ;

we can picture buf as in this figure.

[Illustration of a string]

A few things should be noted about such strings:

The characters following the null byte are garbage; don't count on them to have any specific value.
Every array for holding a string must be at least one byte bigger than the number of characters for the string it's to hold.

Now let's put all this together and write a couple of routines to manipulate strings. First to find the length of a string we just need to count how many characters there are up to (but not including) the null byte.

int strlen( char *p )
   int len = 0 ;

   while( *p != '\0' ) {
      ++len ;
      ++p ;
   return( len ) ;

The astute reader may realize that we can do the same thing in a for loop:

int strlen( char *p )
   int len ;

   for( len = 0; *p != '\0'; ++len, ++p ) ;
   return( len ) ;

Notice that we've put a semicolon at the end of the for statement. This indicates that there is no loop body for this loop. This type of concise programming can be very simple for an experienced programmer, but often causes confusion for beginners. As a result there is some debate over which of these two forms would be preferred stylistically.

As one last exercise and example, consider a function to make a fresh copy of a string:

char *fresh_copy( char *s )
   char *new, *p, *q ;

   new = (char *)malloc( strlen( s ) + 1 ) ;
   for( p = s, q = new; ( *q = *p ) != '\0'; ++p, ++q ) ;
   return( new ) ;

In large part, this function is an exercise because it is very tricky and concise. Study it carefully and make sure you know how it works. Draw a picture and trace through it on a simple example.

Studying string operations is important in order to understand how they work, but because they are so common, many of the ones you'd want are already written for you. The UNIX man page string contains a complete list of the standard ones. Type the command

man string
at the UNIX prompt to see this description. (Warning: Much of the information in this man page is rather technical and will require some careful study. However, once the details are decoded, using the routines is fairly straightforward.)

Return to the index or proceed to the next part.