STRINGS by James Tam (You can find a version of these notes in Unix under: /home/profs/tamj/233/examples/c_examples/string1) A string is merely a one dimensional array of characters. But there are a few differences between a string and other one dimensional arrays. Firstly there are a number of different ways that a string may be initialized when it is declared. e.g., char array1[SIZE]={'h','e','l','l','o'}; char array2[SIZE]="hello"; char array3[]="hello"; Also, unlike other types of arrays, strings must always end with a NULL character (ASCII 0). This is refered to as a NULL terminated string. You might get away without having the NULL at the end of the string for a while but you will get strange results as your program runs and this especially likely if you use the built in string functions in the header library string.h. Typically when using the built in string functions in string.h, the compiler will considerately append a NULL character to the end of all three character arrays for you. However, there is nothing to prevent you from doing something goofy and replacing the NULL with another value so watch out. (If you ignore these cautions then your programs will continuously blow past the bounds of your arrays and you'll end with so many many segmentation faults leading to core dumps on your account it'll make your head spin). Keep in mind when you are debugging code related to your strings /character arrays that some ASCII characters are invisible. When you try to display them to the screen as a character nothing will be displayed. But every character has a numeric ASCII value. An example of an invisible character is the NULL character (ASCII value == 0). So if are displaying the contents of a character array and constantly have an empty screen displayed then your array may consist of invisible characters. You can determine if this is the case or not by displaying the numerical ASCII value of the character rather than trying to display the information as a character. If you are using "printf" then you must use a cast to convert the character to an integer. eg. char ch = 'A'; // The ASCII value of 'A' is 65 (decimal). printf("%c", ch); // This displays the character 'A'. printf("%d %o %x); // This displays the numeric ASCII value // for the letter 'A' in: decimal, octal and // hexadecimal form. Some of the nifty string related functions included in the header file, "string.h" include the following: strlen, strcmp, strncmp, strcat, strncat, strcpy, strncpy. These functions are covered in part 2 of the section of notes on strings, string2.