CIS 251 Exam 3 Name: la. Consider the following C++ code: int a [5] int pl p2 pl
ID: 3837799 • Letter: C
Question
CIS 251 Exam 3 Name: la. Consider the following C++ code: int a [5] int pl p2 pl a; p2 pl 2 *pl *p2 pl++ *pl *p2 Show the values in the array a after the above code executes. Additionally show where pl and p2 are pointing. [0] [1] [2] [3] [4] lb. Continue the above example and write C++ code that does the followi (you are only allowed to use pointers, no indexes point p2 at the last element of the array a add 3 to the value of the element pointed to by pl and place that val in the last element of the array aExplanation / Answer
int a[5]; //Declares a as an array of 5 integers.
int *p1, *p2;
p1 = a; //p1 points to first element of the array, i.e., the address of a[0].
p2 = p1 + 2; //p2 points to 3rd element of the array, i.e., the address of a[2].
*p1 = 3; //a[0] is assigned a value 3.
*p2 = 7; //a[2] is assigned a value 7.
p1++; //p1 now points to second element.
*p1 = *p2 + *a; //a[1] = a[2] + a[0] is what will be executed.
//So, a[1] = 10.
So, after the code:
a[0] = 3;
a[1] = 10;
a[2] = 7;
And the remaining values are garbage.
p1 points to a[1], and p2 points to a[2].
1b.
//Point p2 at the last element of the array a.
p2 = a + 4;
//Add 3 to the value of the element pointed to by p1 and place that
//value in the last element of the array a.
p2 = *p1 + 3;