Professional Documents
Culture Documents
Pointer C++ (UAMD)
Pointer C++ (UAMD)
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 domainthat 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).
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
Aleksandr Moisiu University , Durrs
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 operatorsthe address of
operator (&) and the dereferencing operator (*)to work with pointers. The next two
sections describe these operators.
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.
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 pthat 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
Aleksandr Moisiu University , Durrs
3
Aleksandr Moisiu University , Durrs
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.
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.)
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
Aleksandr Moisiu University , Durrs