by John
Kopp
Welcome to About.com's tutorial on C++ programming. This
lesson covers stings. C++ has two methods for representing
strings, C-style character arrays and the string class.
C-style character arrays are a low-level, primitive
representation of string data. Although the string class
provides more functionality and is less error prone in use, it
is not uncommon to see C-style character arrays in C++ code,
so it is important to understand and be able to use this
representation. As will be seen in a later lesson, this
representation is also used in C++ code to handle command line
arguments. The string class, which is part of the C++ standard
library, provides methods for easy manipulation of string
data. Additional functionality is added by the generic
algorithms introduced earlier in this tutorial.
C-Style Character Arrays As implied by
its name, C-style character arrays is the representation of string data
used in the C programming language. In C, this is the only
technique for storing and manipulating string data. Stings are
stored as null, '\0', terminated character arrays. This
representation has several weaknesses and should generally be
avoided in C++ programming in favor of the use of the string
class.
- A character array of sufficient size must be
defined or allocated to hold the string. The array must be
at least the length of the string plus one. One byte is
needed to hold the null terminator. It is the programmer's
responsibility to be certain that the array is large enough.
The compile will not issue any warnings or errors if they
size is too small. Errors in array size will result in
run-time errors. The program may crash, behave erratically
or operate incorrectly.
- At times, it is necessary to explicitly add
the null terminator.
- Pointers are commonly needed and used to
access and manipulate the string data.
- When copying strings, the programmer must
check that the destination array is large enough. When
adding to strings, again, the array size must be considered.
WAIT. Before you skip immediately to the
next section, there are several important reasons to study and
learn about C-style strings. First, if you need to maintain
any legacy C++ code as part of a job, you will encounter
C-style strings. Some programmers do use them. Additionally,
the string class was not part of the library of early versions
of C++. Programmers typically built their own string classes
or used C-style strings. In either case, knowledge of C-style
strings in necessary. The final reason to learn this
representation is that it is used in C++ to handle command
line arguments. Command line arguments are data or
instructions that are passed into a program when it begins
execution. These arguments are passed into a C++ program as
C-style character arrays. Command line arguments will be
covered in detail in a later lesson.
The C tutorial contains a lesson
that describes the use of C-style strings. I recommend reading
this lesson
to understand C-style strings. C++ is a superset of C. That
is, it contains all of the C language plus much more. A valid
C program is also a valid C++ program. Everything in the C
lesson will be understandable from what you've learned in the
C++ tutorial. Next
page > The
String Class > Page 1,
2,
3,
4,
5
|