Chapter 9 Strings Visual C++ and MFC Fundamentals
9.1 Fundamentals of Strings..............................................................................
9.1.1 Null-Terminated Strings.....................................................................
A string is a group of printable letters or symbols. The symbol can be used as a single
character or some symbols can be combined to produce a word or a sentence. The
symbols are aligned in the computer memory in a consecutive manner so the last symbol
is the null character. Here is an example:
M a t h e m a t i c s \ 0
Such a value is called a null-terminated string because the last symbol is the null-
terminated character "\0". When allocating space for such a string, the space must be
provided as the number of its characters + 1.
Like most other variables, to use a string, you can first declare a variable that would hold
it. To declare a string variable, you can use the char data type and create an array of
symbols. Here are two examples:
char StrName[20];
char *Sentence;
The easiest way to initialize one of these variables is to give it a value when the variable
is declared. Once it has been initialized, such a variable can be used in any function that
can take advantage of it. Here is an example:
void CExoView::OnDraw(CDC* pDC)
{
char *StrName = "Euzhan Palcy";
pDC->TextOut(20, 22, StrName, 12);
}
When a string variable has been initialized, the string the variable holds is called its
value. In the example above, Euzhan Palcy is the string value, or simply the value, of the
StrName variable.
When writing applications for Microsoft Windows, you can also declare a string using
the CHAR data type. Here is an example: