Online Book Reader

Home Category

Professional C__ - Marc Gregoire [13]

By Root 1283 0
to the variable with the value 8

C++ has a special syntax for dealing with pointers to structures. Technically, if you have a pointer to a structure, you can access its fields by first dereferencing it with *, then using the normal . syntax, as in the code that follows, which assumes the existence of a function called getEmployee().

EmployeeT* anEmployee = getEmployee();

cout << (*anEmployee).salary << endl;

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 integer 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 mechanism, called references, which is covered later in this chapter.

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 header file. The details of the standard library are not covered here because C++ provides 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 header file, acts just like a basic type. Just like I/O streams, the string type lives in the “std” namespace. The example that follows shows how strings can be used just like character arrays.

string myString = "Hello, World";

cout << "The value of myString is " << myString << endl;

Code snippet from stringtest\stringtest.cpp

The magic of C++ strings is that you can use standard operators to work with them. Instead of using a function, like strcat() in C to concatenate two strings, you can simply use +. If you’ve ever tried to use the == operator to compare two C-style strings, you’ve discovered that it doesn’t work. == when used on C-style strings is actually comparing the address of the character arrays, not their contents. With C++ strings, == actually compares two strings. The example that follows shows some of the standard operators in use with C++ strings.

Return Main Page Previous Page Next Page

®Online Book Reader