Create a new project named lab6_1. Read the steps for each portion of this lab. Diagram the memory as outlined in the steps (don’t need to turn this part in). Then, write the code to verify your written work. Declare int variables x and y and int* pointer variables p and q. Set x to 2, y to 8, p to the address of x, and q to the address of y. Then print the following information: The address of x (use the address operator) and the value of x. For example, that output statement may look like: cout << "&x = " << &x << ", x = " << x << endl; The value of p and the value of *p. The address of y (use address operator) and the value of y. The value of q and the value of *q. The address of p (not its contents, don’t dereference). The address of q (not its contents, don't dereference). How are the addresses of x, y, p, and q related? List them out and find the distances between each address. You can include these answers in your program as a block comment. Comment out the previous code. Now, declare int variables x, y, temp and int* pointer variables p, q, temp_ptr. Set x, y, temp to three distinct values. Set p, q, temp_ptr to the addresses of x, y, temp respectively. Print, with labels, the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr. Print the message: Swapping values. Then, use the following swap code: temp = x; x = y; y = temp; Print with labels the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr. What was the effect of the swap code? Which values changed? You can include these answers in your program as a block comment. Comment out the previous code. Declare int variables x, y, temp and int* pointer variables p, q, temp_ptr. Set x, y, temp to three distinct values. Set p, q, temp_ptr to the addresses of x, y, temp respectively. Print, with labels, the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr. Print the message: Swapping pointers. Then, use the following swap code: temp_ptr = p; p = q; q = temp_ptr; Print with labels the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr. What was the effect of the swap code? Which values changed? You can include these answers in your program as a block comment.
- Create a new project named lab6_1. Read the steps for each portion of this lab. Diagram the memory as outlined in the steps (don’t need to turn this part in). Then, write the code to verify your written work.
-
Declare int variables x and y and int* pointer variables p and q. Set x to 2, y to 8, p to the address of x, and q to the address of y. Then print the following information:
-
The address of x (use the address operator) and the value of x.
For example, that output statement may look like:
cout << "&x = " << &x << ", x = " << x << endl; -
The value of p and the value of *p.
-
The address of y (use address operator) and the value of y.
-
The value of q and the value of *q.
-
The address of p (not its contents, don’t dereference).
- The address of q (not its contents, don't dereference).
-
How are the addresses of x, y, p, and q related? List them out and find the distances between each address. You can include these answers in your program as a block comment.
-
-
Comment out the previous code. Now, declare int variables x, y, temp and int* pointer variables p, q, temp_ptr. Set x, y, temp to three distinct values. Set p, q, temp_ptr to the addresses of x, y, temp respectively.
- Print, with labels, the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr.
-
Print the message: Swapping values.
- Then, use the following swap code:
- temp = x;
- x = y;
- y = temp;
- Print with labels the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr.
- What was the effect of the swap code? Which values changed? You can include these answers in your program as a block comment.
- Comment out the previous code. Declare int variables x, y, temp and int* pointer variables p, q, temp_ptr. Set x, y, temp to three distinct values. Set p, q, temp_ptr to the addresses of x, y, temp respectively.
- Print, with labels, the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr.
-
Print the message: Swapping pointers.
-
Then, use the following swap code:
- temp_ptr = p;
- p = q;
- q = temp_ptr;
-
Print with labels the values of x, y, temp, p, q, temp_ptr, *p, *q, *temp_ptr.
- What was the effect of the swap code? Which values changed? You can include these answers in your program as a block comment.
-
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images