It dereferences a pointer, allowing code to work with the value that the pointer
points to.
Look at the following code.
int x = 7;
int *iptr = &x;
What will be displayed if you send the expression *iptr to cout ? What happens if you send the
expression ptr to cout ?
The value 7 will be displayed if the expression *iptr is sent to cout. If the
expression iptr is sent to cout, the address of the variable x will be displayed.
Multiplication operator, definition of a pointer variable, and the indirection
operator
Addition and subtraction.
It adds 4 times the size of an int to the address stored in ptr
Look at the following array definition.
int numbers[] = { 2, 4, 6, 8, 10 };
What will the following statement display?
cout «_space;*(numbers + 3) «_space;endl;
8
To dynamically allocate memory.
What happens when a program uses the new operator to allocate a block of memory, but the
amount of requested memory isn’t available? How do programs written with older compilers
handle this?
An exception is thrown, which causes the program to terminate. Under older
compilers, the new operator returns the null address (address 0) when it cannot
allocate the requested amount of memory.
To free memory that has been dynamically allocated with the
You should only return a pointer from a function if it is
* A pointer to an object that was passed into the function as an argument
* A pointer to a dynamically allocated object
A pointer to a constant points to a constant item. The data that the pointer points
to cannot change, but the pointer itself can change. With a constant pointer, it is
the pointer itself that is constant. Once the pointer is initialized with an address, it
cannot point to anything else.
Not only will this protect you from writing code in the function that accidentally
changes the argument, but the function will be able to accept the addresses of both
constant and non-constant arguments.
address
address (&)
pointer
indirection (*)
pointers
dynamic memory allocation
new
0 or null
null
delete
new
Look at the following code.
double value = 29.7;
double *ptr = &value;
Write a cout statement that uses the ptr variable to display the contents of the value variable.
cout «_space;*ptr «_space;endl;