![]() ![]() In this illustration, we use "T" to represent an unspecified data type because the results are independent of the kind of data used. It's easier to understand how general operators behave when applied to pointers if we keep in mind the relationship between the pointer and the data to which it points. For this discussion, we'll call operators that are not specifically intended to work with pointers general operators. You can get a good sense of how most operators work with pointers if you recall how pointers "look." T data However, not all operators produce meaningful or useful results when applied to pointers. Many of the C++ operators introduced in previous chapters will "work" with pointers in the sense that they will compile and run without errors. When using a picture to understand or solve a problem that uses pointers, it is very common to represent the value stored in a pointer variable as an arrow from the pointer variable to the data to which it points or refers. An alternate view of (b): For the sake of illustrating what is taking place in memory, the address stored in p is replaced by an arrow pointing from p to i.p and &i are assignment compatible because both sides of the assignment operator represent an address that is, the assignment operation is possible because pointers are variables that store addresses. ![]() The ampersand is the address of operator it forms an expression with the variable immediately to its right that calculates the address of the variable. The content of the variables change but the addresses remain the same. When defining a pointer variable there may be a space on either or both sides of the asterisk: int* p, int *p, or int * p. Both variables have an address but the contents are undefined. The compiler allocates memory to hold the variables and maps their names to those memory locations. In this context, the asterisk defines p as a pointer variable specifically, a variable that holds the address of an integer. This row provides an alternate image for the code illustrated in row (b) Subsequent sections in this chapter describe the other operators. For simplicity, the demonstration uses the integer variable i. The address-of-operator may be used with any variable regardless of its type. It shows how to define a pointer variable and how to find the address of an existing variable. The next figure illustrates two operations with pointers. ![]() The compiler is able to distinguish which operator each character represents by its context (i.e., where it is used), which implies that programmers must also learn how to distinguish between operators based on their context. Three pointer operators and their contextual meaning. When used in an expression, is the dereference operator (also known as the indirection operator) When used in a variable definition, defines a pointer variable And second, it also serves as the dereference or the indirection operator (both name the same operation), which we take up in greater detail in the next section. ![]() First, the asterisk defines a pointer variable. To make matters worse, besides its use as the multiplication operator, the asterisk, *, has two uses just within the context of pointers. Furthermore, the chapter 2 supplemental section introduces the single ampersand operator as the bitwise AND operator. Recall from the previous chapter, two ampersands together also forms the logical AND operator. For example, the ampersand, &, forms the address of operator. The compiler distinguishes between the meanings based on context (i.e., where the symbols appear in a program). Unfortunately, there are a limited number of characters on a keyboard, and all of the symbols used to implement the single-character pointer operators have other overloaded meanings. Two of the operators are formed by complete words: new and delete one operator consists of two characters without any spaces: ->, and the remaining three operators consist of a single character. Once we have the variable's address, we can work with it indirectly through the pointer.Īlthough there are many operators used with pointers, six are common. Although we can't control a variable's placement in memory, we can find its address with one of the pointer operators and save it in a pointer variable. Specifically, when we draw pictures of pointers, we often replace a specific address (the contents of a pointer variable) with an arrow: The arrow points to the data whose address the pointer stores. Programmers generally don't know (and have no control over) where the compiler and the operating system place variables in memory, so we tend to think about pointers in rather general terms. Pointers allow us to work more directly with a computer's memory. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |