lect8_3

lect8_3 - Pointers and Character Strings 1 Pointers and...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Pointers and Character Strings 1 Pointers and Character Strings A character enclosed between single quotes is a character constant . For example, A represents the character A . We have seen before that each character constant is represented in the computer with an integer (ASCII) value. For example, the ASCII value of A is 65 , the ASCII value of the newline character \n is 10 . A string is a sequence of characters treated as a single unit. A string may include letters, digits, punctuations, and others. In C++, a string constant (also called a string literal ) is enclosed between double quotation marks. For example, "John Doe" , "CS 115 Class" , and "201-216-5614" are string literals. Every string literal in C++ is an array of characters ending in the null character \0 . The end marker \0 enables a program to read the string one character at a time and know that it should stop reading when it reads the end marker. A string stored in this way (as a sequence of characters terminated with \0 ) is called a C-string . We write \0 with two symbols in a program, but just like the newline character \n , the character \0 is only a single character value. Like other character values, \0 can be stored in one variable of type char . A string may be assigned in a declaration to a character array as follows. char A = "Hello"; This declaration creates an array with 6 elements containing the characters H , e , l , l , o , \0 , which is equivalent to the following. char A = {H, e, l, l, o, \0}; Pointers and Character Strings 2 Note that the following two declarations are not equivalent. char A = "Hello"; // <----- (1) char A = {H, e, l, l, o}; // not same as (1) The declaration char A = "Hello"; places the null character \0 at the end of the string as shown below. The declaration char A = { H,e,l,l,o } ; will not place the null char- acter. See figure below. When we initialize a C-string variable, we can omit the array size as follows. char A = "Hello"; The compiler automatically make the size of the array one more than the length of the quoted string. The one extra element is for \0 . When we initialize a character array with size, the size must be large enough to store the string and its terminating null character. For example the following declartion is adequate. char A[9] = "Hello"; The resulting array looks like the following. Pointers and Character Strings 3 Input and output with strings We have been using the insertion operator << to output C-strings using statements such as the following....
View Full Document

This note was uploaded on 04/07/2008 for the course CS 181 taught by Professor Satya during the Fall '08 term at Stevens.

Page1 / 11

lect8_3 - Pointers and Character Strings 1 Pointers and...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online