Slide 8- 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
An Array Type for Strings
C-strings can be used to represent strings of
characters
C-strings are stored as arrays of characters
C-strings use the null character '\0' to end a
string
The Null character is a single character
To declare a C-string variable, declare an array
of characters:
char s[11];
Slide 8- 6
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Declaring a C-string as char s[10] creates space
for only nine characters
The null character terminator requires one
space
A C-string variable does not need a size variable
The null character immediately follows the last
character of the string
Example:
??\0!moMiH
s[9]s[8]s[7]s[6]s[5]s[4]s[3]s[2]s[1]s[0]
C-string Details
Slide 8- 7
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
C-string Declaration
To declare a C-string variable, use the syntax:
char Array_name[ Maximum_C_String_Size + 1];
+ 1 reserves the additional character needed
by '\0'
Slide 8- 8
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Initializing a C-string
To initialize a C-string during declaration:
char my_message[20] = "Hi there.";
The null character '\0' is added for you
Another alternative:
char short_string[ ] = "abc";
but not this:
char short_string[ ] = {'a', 'b', 'c'};
Slide 8- 9
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
C-string error
This attempt to initialize a C-string does not
cause the \0 to be inserted in the array
char short_string[ ] = {'a', 'b', 'c'};
Slide 8- 10
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Don't Change '\0'
Do not to replace the null character when
manipulating indexed variables in a C-string
If the null character is lost, the array cannot act
like a C-string
Example: int index = 0;
while (our_string[index] != '\0')
{
our_string[index] = 'X';
index++;
}
This code depends on finding the null character!
Slide 8- 11
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Safer Processing of C-strings
The loop on the previous slide depended on
finding the '\0' character
It would be wiser to use this version in case the
'\0' character had been removed
int index = 0;
while (our_string[index] != '\0'
&& index < SIZE)
{
our_string[index] = 'X';
index++;
}
Slide 8- 12
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Assignment With C-strings
This statement is illegal:
a_string = "Hello";
This is an assignment statement, not an
initialization
The assignment operator does not work with
C-strings
Slide 8- 13
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Assignment of C-strings
A common method to assign a value to a
C-string variable is to use strcpy, defined in
the cstring library
Example: #include <cstring>
…
char a_string[ 11];
strcpy (a_string, "Hello");
Places "Hello" followed by the null character in
a_string
Slide 8- 14
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
A Problem With strcpy
strcpy can create problems if not used carefully
strcpy does not check the declared length of
the first argument
It is possible for strcpy to write characters
beyond the declared size of the array
Không có nhận xét nào:
Đăng nhận xét