A pointer is a type of variable that holds the address of another variable. In other words, it "points to" another varaible. The following video will show you how to create a pointer, as well as dereference pointers, and show you how to access the address of a variable.
Pointers can be used as struct members to avoid the build issues we saw last week. Take a look.
Review Questions
The following are examples of questions similar to what you can expect to see on the next exam.
- What is the syntax for creating a pointer? data-type *name;
- What is the ampersand (&) used for when not in a parameter list? Getting the address of a variable. It's the "Address of" operator.
-
What does the following code do?
int *pNumber;
Creates a pointer to an integer. -
What does the following code do?
pNumber = &number;
Gets the address of "number" and assigns it to "pNumber." -
What does the following code do?
cout << *pNumber;
Prints the value stored at the location that pNumber points to. -
What is wrong with the following code?
int *pNumber = &10;
10 is not a variable (it's a literal), therefore you cannot get the address of it.