EECS168 09:Lab12
| Navigation | 
|---|
| Home | 
| Information | 
| Classwork | 
Objectives
- Pointer, & operator and * operator
 - Dynamic array
 
& operator
The & operator in front of an ordinary variable produces the address of that variable. If we have a variable x of type int, then &x gives us the address of x.
Write a program, called address.cpp. The program should prompt the user to enter an integer, store it in an integer variable, then print out both the address of and the value of that variable. Output of your program should look like below:
Enter an integer to store in x: 1
Address of x is: 0xbfcf81fc
Value of x is: 1
* operator
The * operator in front of a pointer variable produces the variable it points to. If we have an integer pointer p (int *p), then *p gives us the integer that p points to.
Create a file called pointer.cpp which contains the following code.
double var1, *p1;
cout<< "enter a number to put into var1:";
cin>>var1;
p1 = &var1;
cout<< "var1 holds the value: "<< var1<< " which should be the same as "<< *p1 << endl;
cout<< " p1 points to the memory address "<< p1 << "which holds the value of "<< *p1<< endl;
	
*p1 = 23;
cout<< "var1 is now changed to: "<< var1 << endl;
Run the program to see how it works. Add another double pointer to the code, assign the value of p1 to the new pointer. Make your program print the value of the new poniter and the value that it points to.
new and delete operators
The new operator creates a dynamic variable of a specified type and returns a pointer that points to that variable. This new operator is used to create variables that have no identifiers to name them. The delete operator eliminates a dynamic variable. For example:
int* x = new int;
...
delete x;
will create a dynamic int variable, the pointer x points to that variable. The new, nameless variable can be referred to as *x.
Write a program called new.cpp. The program should prompt the user to enter an integer, store it in a dynamic integer that x points to, then print out both x and the integer that x points to. Make sure that your program has only one variable x; use delete operator to eliminate x when you don't use it any more.
Output of your program should looks like :
Enter an integer to store in *x (x points to a dynamic integer): 1
Value of x is: 0x9f2f008
The integer x points to is: 1
Dynamic array
A dynamic array is an array whose size is not specified when you write the program, but it is determined while the program is running. The below program creates a dynamic array of type double. After creating and using an array, you can use delete[] to eliminate the array.
    double* array;
    int size;
    ...
    array = new double[size];
    ...
    delete[] array;
Write a program, called dynamicArray.cpp. The program should prompt the user to enter some numbers, fill them in a dynamic array, and then print them out. Before asking the user to enter numbers, the program first asks the user to decide how many numbers to enter, so the program can create a dynamic array with the exact size needed.
Output of your program should looks like below:
How many number do you want to enter?: 3
Enter 3 numbers: 1.2 6.3 8.4
All numbers you entered are: 1.2 6.3 8.4 
Lab Submission
- address.cpp
 - pointer.cpp
 - new.cpp
 - dynamicArray.cpp
 
The submission procedure is the same as was described in previous labs, except that:
- Your tarball and the directory inside it should be named like Smith-1234567-Lab-12
 - The email subject should be "[EECS 168] Lab 12" (without the quotes).
 
Remember to include your full name, student number, and disk usage in the email.