The main memory (or simply the memory) is where variables and other information are stored while a program runs. Sometimes it is called the RAM, for random-access memory. From the perspective of a program, the computer's memory is a collection of bytes, each with an integer address. For example, there is a byte with address 0, one with address 1, one with address 2, etc., up to a very large number. A program can fetch the current contents of the byte at a given memory address and it can store a given value into that byte of memory.
A byte is just 8 bits. Most of data items are larger than that. For example, a value of type int is usually 32 bits, so it occupies 4 bytes.
A program refers to a block of memory using the address of the first byte in the block. For example, an integer stored in bytes 1000-1003 has address 1000.
A memory address is called a pointer because you can think of it as pointing to a specific spot in memory.
From a machine language perspective, a pointer is the same as a long integer (32 bits on a 32-bit machine, 64 bits on a 64-bit machine.) A program can treat a pointer as information in the same way that it treats an integer as information.
But C++ treats pointers a little differently from the way they are treated in machine language. Each pointer has a type that tells the type of thing in the memory that it points to. To write the type of a pointer, write an asterisk after another type. For example,
A value of type int* is a pointer to a location in memory that holds a value of type int. If pointer variable p has type int* and holds address 1000, then the memory holds an integer in addresses 1000-1003.
A value of type double* is a pointer to a location in memory that holds a value of type double.
A value of type char* is a pointer to a location in memory that holds a value of type char. So it refers to a single byte.
A value of type char** is a pointer to a location in memory that holds a value of type char*. That is, it points to another pointer. You can add a * to any type, including a pointer type.
If you create only one pointer variable in a statement, then creating a pointer variable is just like creating any other variable. For example,
int* q;creates a variable called q of type int*. Like other variables, no initial value is stored into q automatically. So q starts out holding a junk pointer.
If you create two or more pointer variables in one statement, you need to be aware of a peculiarity of C++: Each variable that is a pointer needs to have a * in front of it. Statement
char *r, *s;creates variables r and s, each of type char*. Statement
char *r, s;creates variable r of type char* and s of type char. It does not matter where you put spaces. Statement
char* r, s;still makes s have type char, not char*.
If x is a variable then &x is the address where x is stored. So int v; int* p = &v;makes variable p hold the address of variable v. Notice that, if v has type T, then &v has type T *. |
*p
If p is a pointer then *p is the variable
to which p points. For example,
int v = 1; int* p = &v; *p = 2;ends with v = 2, since *p is the same a v. If p has type T * then *p has type T. For example, pointer p above has type int*. So *p has type int. Notice that you write * after a type but before a variable. Here is a way to remember that. In declaration int* p;the star comes between type int and variable p. That is, star comes after the type and before the variable. You can think of that as saying that p has type int*. But you can also write int *p;which seems to say that *p has type int. Both are correct. |
q = p;
If you set pointer variable q equal to pointer variable p,
then it makes q hold the same memory address as p.
So q and p point to the same place.
It does not make q point to p. |
*q = *p;
Remember that *p is the variable to which p points.
Assignment *q = *p sets the variable
to which q points
equal to the variable to which p points. This is
not at all the same as assignment q = p.
For example, after
int x = 1, y = 2; int* p = &x int* q = &y; *q = *p;variable y holds 1, since *p is the same as x and *q is the same as y. |
A pointer is a memory address.
Operators & and * are opposites of one another.
&x is the memory address where variable x is stored.
If p is a pointer then *p is the variable to which p points.
If x has type T then &x has type T*. Adding an & to a variable adds a * to its type.
If p has type T* then *p has type T. Adding an * to a variable removes a * from its type.
If p is a pointer then expression &(*p) is the same as p. For any variable x, *(&x) is the same as x. The & and * operators are opposites. They undo one another.
If p and q are pointer variables then
p = q;copies the memory address that is in q into p. It makes p point to the same place as q. If p points to variable a and q points to variable b. Then
*p = *q;copies the value in b into variable a.
What sort of thing is stored in a variable of type long*? Answer
Suppose that w is a variable of type long. Write a statement that creates a variable q of type long* and makes q hold the address of variable w. Answer
int r = 0; long* p = &r;allowed? Answer
long r = 0; long* p = r;allowed? Answer
Suppose that p is a pointer of type int*. Write a statement that stores 25 into the variable to which p points. Answer
Suppose that variable p has type Widget*. What is the type of expression *p? Answer
Suppose that variable w has type Widget. What is the type of expression &w? Answer