C Programming/Strings

Objective

 * Learn more about arrays, char arrays, and strings.

= String as char array =

There is no string type in C. Instead we have arrays or constants, and we can use them to store sets of characters. The string  can be stored like in the example below.

This creates an array of chars so that  is 'H',   is 'e', etc. In addition, because C allows us to walk off the end of arrays there is a final, terminating character   which is '\0'.

Hypothetically one could create  using:

= String as char pointer =

C-Style strings variables are often dealt with not as an array of chars, but rather as a pointer to the first element of an array of chars sitting in memory. The type "char *" (pronounced char star) is used for this.

The value of spointer is a something like 1386120007. This is a memory address. Literally this means that the data we want is at the 1386120007th byte in memory. We can use the * (star) operator to inspect the data at that address. We find that it is the 'H' in "Hello World!". To get at the 'H' we dereference the pointer using the star operator.

We see that to get at the next value we can simply look at the next memory address (spointer + 1). This is because our string is stored sequentially in memory (unlike in a linked list for example).

Using this memory-address arithmetic and the string terminator discussed above we can very easily walk down a string, doing something at each character:

= String Pointers Pretending to be Arrays =

In the last section we saw that we could access the ith element of a string using

In C this can be rewritten in Array Syntax as follows:

Please note that even though this looks and behaves like an array of characters, it is not. This distinction can cause a lot of confusion for students just starting with pointers. It is recommended that you use the  method for your first few assignments.

If we include string.h, and use the  function to obtain the length of the string and use for loops and structures that may be more familiar to those coming from other languages. The example below also replaces all letters with 'a'.

Beware! strlen returns the length of the string not including the terminating character '\0' . It is very important that you always terminate any string you create, or it will continue through other memory until it finds a '\0'. If you modify some other program's memory your program may crash.

This array-style method is probably more familiar to many programmers not accustomed to pointers. It is strongly recommended however that students new to C do not use this method until they understand pointers. While convenient it hides what is happening inside the computer. Additionally, this method is slower because it looks through the string multiple times (first to determine the length in strlen, and then iterating over it in the for loop).

= Allocating Strings Dynamically =

When creating static strings like "Hello World!" the compiler can allocate the space ahead of time. If your program needs to create a string of varying lengths then you'll have to allocate the memory yourself using malloc.

In duplicating a string, s, for example we would need to find the length of that string:

And then allocate the same amount of space plus one for the terminator and create a variable that points to that area in memory:

This creates the appropriate amount of space and gives us a pointer to the location of the first character. Now we just need to copy the string s over, character by character. This could be accomplished with the while or for loop methods described above.

Assignments



 * Previous Lesson: Pointers
 * Next Lesson: Structs
 * Course Home Page