Name:
Sec: 06/26/2008
The values belonging to pointer data types are the memory addresses (memory locations).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.The following is the general syntax of declaring a pointer variable:
dataType* identifier;
or
dataType* identifier;
In C++, the ampersand (&) is called the address of operator, and is a unary operator that returns the address of its operand.
In C++, the asterisk character (*) has multiple uses. When used as a unary operator, * is called the dereferencing operator, and refers to the object to which its operand (that is, a pointer) points. For example, consider the following statements:
int x =25;
int *p;
p = &x; // store the address of x in p
cout << *p << endl;
These statements print the value stored in the memory space pointed to by p,which is the value of x.
In this assignment you will use pointers, the address of operator (&), and the dereferencing operator (*) to access and store date into variables.
After completing this assignment, you will be able to:
Use pointer variables
Use the address of operator (&).
Use the dereferencing operator (*)
Design a program to calculate the cost of gas for a trip. The user will be prompted for the cost of a gallon of gas, the number of miles of the trip, and the number of miles per gallon the car gets. The program should use pointers, the address operator, and the dereferencing operator for all input, calculations, and output.
The following is a copy of the screen results that might appear after running your program. Input by the user is shown in bold.
This program calculates the cost of gas for a trip when the user enters the cost of gas, the number of miles, and the number of miles per gallon
the vehicle gets.
Enter the number of miles in your trip: 890
Enter the mpg your car gets: 23
Enter the cost of gasoline: 1.79
Your trip will cost $68.02
You have now completed Assignment 4. If your program compiles and runs correctly and you have successfully uploaded your source file to the eCollege online submission site, then you are done.