String and Character Array

String is a sequence of characters that are treated as a single data item and terminated by a null character ' $\backslash$0'. Remember that the C language does not support strings as a data type. A string is actually a one-dimensional array of characters in C language. These are often used to create meaningful and readable programs.

For example: The string "home" declared by,

$\displaystyle char~s[] = \lq\lq home'';$

contains 5 characters including the ' $\backslash$0' character which is automatically added by the compiler at the end of the string.

Figure: Char Array in memory
Image arrcm

The Fig. [*] shows that char array s[] stores h, o, m, e in the array elements s[0], s[1], s[2], s[3], respectively, and stores ` $\backslash 0$' in the element s[4]. The corresponding memory locations are also shown in this figure, not that difference between memory addresses is 1 for character elements. The %p is format specifier for pointer (pointer in C language means address of some memory location).

Figure: Accessing Char Array from memory, and printing its addresses
Image arrcm1