Datasheet

This syntax is a little messy. The -> (arrow) operator lets you perform both the dereference and the field
access in one step. The following code is equivalent to the preceding code, but is easier to read.
EmployeeT* anEmployee = getEmployee();
cout << anEmployee->salary << endl;
Normally, when you pass a variable into a function, you are passing by value. If a function takes an inte-
ger parameter, it is really a copy of the integer that you pass in. Pointers to stack variables are often used
in C to allow functions to modify variables in other stack frames, essentially passing by reference. By
dereferencing the pointer, the function can change the memory that represents the variable even though
that variable isn’t in the current stack frame. This is less common in C++ because C++ has a better mech-
anism, called references, which is covered below.
Strings in C++
There are three ways to work with strings of text in C++. There is the C-style, which represents strings as
arrays of characters; the C++ style, which wraps that representation in an easier-to-use string type; and
the general class of nonstandard approaches.
C-Style Strings
A string of text like “Hello, World” is internally represented as an array of characters with the character
‘\0’ representing the end of the string. As you’ve seen, arrays and pointers are sometimes related. You
could use either one to represent a string, as shown here:
char arrayString[20] = “Hello, World”;
char* pointerString = “Hello, World”;
For the arrayString, the compiler allocates space for 20 characters on the stack. The first 13 characters
in the array are filled in with
‘H’, ‘e’, etc., ending with the character ‘\0’. The characters in positions
13 to 19 contain whatever random values happen to be in memory. The
‘\0’ character tells code that
uses the string where the content of the string ends. Even though the array has a length of 20, functions
that process or output the string should ignore everything after the
‘\0’ character.
For the
pointerString, the compiler allocates enough memory on the stack just to hold the pointer.
The pointer points to an area of memory that the compiler has set aside to hold the constant string
“Hello, World.” In this string, there is also a
‘\0’ character after the ‘d’ character.
The C language provides a number of standard functions for working with strings, which are described
in the <
cstring> header file. The details of the standard library are not covered here because C++ pro-
vides a much cleaner and simpler way of working with strings.
C++ Strings
C-style strings are important to understand because they are still frequently used by C++ programmers.
However, C++ includes a much more flexible string type. The
string type, described by the <string>
header file, acts just like a basic type. Just like I/O streams, the
string type lives in the “std” package.
The example that follows shows how strings can be used just like character arrays.
21
A Crash Course in C++
04_574841 ch01.qxd 12/15/04 3:39 PM Page 21