In this chapter, we discuss structures and the manipulation of bits, characters and C-style strings. Many of the techniques we present here are included for the benefit of the C++ programmer who will work with C, and early C++, legacy code.
The designers of C++ evolved structures into the notion of a class. Like a class, C++ structures can contain access specfiers, member functions, constructors and destructors. In fact, the only difference between structures and classes in C++ is that structure members default to public access and class members default to private access when no access specifiers are used. Classes have been covered thoroughly in the book, so there is really no need for us to discuss structures in detail. Our presentation of structures in this chapter focuses on their use in C, where structures contain only public data members. This use of structures is typical of the legacy C code and early C++ code you'll see in industry.
We discuss how to declare structures, initialize structures and pass structures to functions. Then, we present a high-performance card shuffling and dealing simulation in which we use structure objects and C-style strings to represent the cards. We discuss the bitwise operators that allow programmers to access and manipulate the individual bits in bytes of data. We also present bitfieldsspecial structures that can be used to specify the exact number of bits a variable occupies in memory. These bit manipulation techniques are common in C and C++ programs that interact directly with hardware devices that have limited memory. The chapter finishes with examples of many character and C-style string manipulation functionssome of which are designed to process blocks of memory as arrays of bytes.