Pointer Data Type and Pointer Variables The values belonging to pointer data types are the memory addresses of your computer. As in many other languages, there is no name associated with the pointer data type in C++. Because the domain—that is, the set of values of a pointer data type - is the addresses (memory locations), a pointer variable is a variable whose content is an address, that is, a memory location. Pointer variable: A variable whose content is an address (that is, a memory address).
Declaring Pointer Variables
As remarked previously, there is no name associated with pointer data types. Moreover, pointer variables store memory addresses. So the obvious question is: If no name is associated with a pointer data type, how do you declare pointer variables? The value of a pointer variable is an address. That is, the value refers to another memory space. The data is typically stored in this memory space. Therefore, when you declare a pointer variable, you also specify the data type of the value to be stored in the memory location pointed to by the pointer variable. In C++, you declare a pointer variable by using the asterisk symbol (*) between the data type and the variable name. The general syntax to declare a pointer variable is: dataType *identifier; As an example, consider the following statements: int *p; char *ch; In these statements, both p and ch are pointer variables. The content of p (when properly assigned) points to a memory location of type int, and the content of ch points to a memory location of type char. Usually, p is called a pointer variable of type int, and ch is called a pointer variable of type char. Before discussing how pointers work, let us make the following observations. The statement: int *p; is equivalent to the statement: int* p; which is equivalent to the statement: int * p; Thus, the character * can appear anywhere between the data type name and the variable name. Now, consider the following statement: int* p, q; In this statement, only p is the pointer variable, not q. Here, q is an int variable. To avoid confusion, we prefer to attach the character * to the variable name. So the preceding statement is written as: int *p, q; Of course, the statement: int *p, *q; declares both p and q to be pointer variables of type int. Now that you know how to declare pointers, next we will discuss how to make a pointer point to a memory space and how to manipulate the data stored in these memory locations. 1 “Aleksandër Moisiu” University , Durrës
Lecturer: Msc. Kristel BOZHIQI
Because the value of a pointer is a memory address, a pointer can store the address of a memory space of the designated type. For example, if p is a pointer of type int, p can store the address of any memory space of type int. C++ provides two operators—the address of operator (&) and the dereferencing operator (*)—to work with pointers. The next two sections describe these operators.
Address of Operator (&)
In C++, the ampersand, &, called the address of operator, is a unary operator that returns the address of its operand. For example, given the statements: int x; int *p; the statement: p = &x; assigns the address of x to p. That is, x and the value of p refer to the same memory location.
Dereferencing Operator (*)
Every chapter until now has used the asterisk character, *, as the binary multiplication operator. C++ also uses * as a unary operator.When used as a unary operator, *, commonly referred to as the dereferencing operator or indirection operator, refers to the object to which its operand (that is, the pointer) points. For example, given the statements: int x = 25; int *p; p = &x; //store the address of x in p the statement: cout << *p << endl; prints the value stored in the memory space pointed to by p, which is the value of x. Also, the statement: *p = 55; stores 55 in the memory location pointed to by p—that is, in x. Example 1 Let us consider the following statements: int *p; int num; In these statements, p is a pointer variable of type int, and num is a variable of type int. Let us assume that memory location 1200 is allocated for p, and memory location 1800 is allocated for num.
2 “Aleksandër Moisiu” University , Durrës
Lecturer: Msc. Kristel BOZHIQI
Figure1. Variables p and num Consider the following statements. 1. num = 78; 2. p = # 3. *p = 24; The following shows the values of the variables after the execution of each statement.
Let us summarize the preceding discussion. 1. A declaration such as int *p; allocates memory for p only, not for *p. Later, you will learn how to allocate memory for *p. 2. The content of p points only to a memory location of type int. 3. &p, p, and *p all have different meanings. 4. &p means the address of p—that is, 1200 5. p means the content of p, which is 1800, after the statement p = # executes. 6. *p means the content of the memory location to which p points. Note that after the statement p = # executes, the value of *p is 78; after the statement *p = 24; executes, the value of *p is 24.
3 “Aleksandër Moisiu” University , Durrës
Lecturer: Msc. Kristel BOZHIQI
Example 2 Consider the following statements: int *p; int x; Suppose that we have the memory allocation for p and x as shown in Figure 2.
Figure 2. Main memory, p, and x The values of &p, p, *p, &x, and x are as follows:
Suppose that the following statements are executed in the order given: x = 50; p = &x; *p = 38; The values of &p, p, *p, &x, and x are shown after each of these statements executes. After the statement x = 50; executes, the values of &p, p, *p, &x, and x are as follows:
After the statement p = &x; executes, the values of &p, p, *p, &x, and x are as follows:
After the statement *p = 38; executes, the values of &p, p, *p, &x, and x are as follows. (Because *p and x refer to the same memory space, the value of x is also changed to 38.)
Let us note the following: 1. p is a pointer variable. 2. The content of p points only to a memory location of type int. 4 “Aleksandër Moisiu” University , Durrës
Lecturer: Msc. Kristel BOZHIQI
3. Memory location x exists and is of type int. Therefore, the assignment statement: p = &x; is legal. After this assignment statement executes, *p is valid and meaningful. Example 3 #include using namespace std; int main() { int *p; int x = 37; cout << "Line 1: x = " << x << endl; p = &x; cout << "Line 3: *p = " << *p << ", x = " << x << endl; *p = 58; cout << "Line 5: *p = " << *p << ", x = " << x << endl; cout << "Line 6: Address of p = " << &p << endl; cout << "Line 7: Value of p = " << p << endl; cout << "Line 8: Value of the memory location " << "pointed to by *p = " << *p << endl; cout << "Line 9: Address of x = " << &x << endl; cout << "Line 10: Value of x = " << x << endl; return 0; } Sample Run: Line 1: x = 37 Line 3: *p = 37, x = 37 Line 5: *p = 58, x = 58 Line 6: Address of p = 006BFDF4 Line 7: Value of p = 006BFDF0 Line 8: Value of the memory location pointed to by *p = 58 Line 9: Address of x = 006BFDF0 Line 10: Value of x = 58 The preceding program works as follows. The statement in Line 1 outputs the value of x, and the statement in Line 2 stores the address of x into p. The statement in Line 3 outputs the values of *p and x. Because p contains the address of p, the values of *p and x are the same, as shown by the output of Line 3. The statement in Line 4 changes the value of *p to 58, and the statement in Line 5 outputs the values of *p and x, which are again the same. The statements in Lines 6 through 10 output the address of p, the value of p, the value of *p, the address of x, and the value of x. Note that the value of p and the address of x are the same because the address of x is stored in p by the statement in Line 2. (Note that the address of p, the value of p, and the address of x, as shown by the outputs of Lines 6, 7, and 9, 5 “Aleksandër Moisiu” University , Durrës
Lecturer: Msc. Kristel BOZHIQI
respectively, are machine dependent. When you run this program on your machine, you are likely to get different values. Furthermore, the pointer values, that is, addresses, are printed in hexadecimal by default.)
6 “Aleksandër Moisiu” University , Durrës
Lecturer: Msc. Kristel BOZHIQI