Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following codes and answer a, b, c (assume proper includes, etc): i

ID: 3793323 • Letter: C

Question

Consider the following codes and answer a, b, c (assume proper includes, etc):

int main() {

(a) Which statements of the code marked for part (a) are valid, and which statements are invalid? If a statement is invalid, please explain why.

(b) Which statements of the code marked for part (b) are valid, and which statements are invalid? If a statement is invalid, please explain why.

(c) Which statements of the code marked for part (c) are valid, and which statements are invalid? If a statement is invalid, please explain why.

} // foo

} // bar

(a) Which statements of the code marked for part (a) are valid, and which statements are invalid? If a statement is invalid, please explain why.

(b) Which statements of the code marked for part (b) are valid, and which statements are invalid? If a statement is invalid, please explain why.

(c) Which statements of the code marked for part (c) are valid, and which statements are invalid? If a statement is invalid, please explain why

Explanation / Answer

For first program:

(a) All three are valid statements and they will return the first element of array.

int * p1 = a;

const int * p2 = a; -> You can change the data it is pointing to but cannot point to a different memory location.

const int * const p3 = a; -> You can not change the data it is pointing to as well as you can not change the memory location.

(b) First two statments are valid.

Initially both were pointing to the first element of the array. Now it will increment the base address depending upon the data type and compiler and will now point to the second element of the array i.e. 2

Is an invalid statement. Because both the memory address and data it is pointing to is constant and you can not change it and hence it will give you a compilation error.

(c) Only third statement is correct.

(*a)++ -> This will increment the first element of array by 1 i.e the output is 2

First two are invalid

a=b -> You can not assign one array to another like this.It will give you a compilation error as: invalid array assignment

a++ -> This will give you an lvalue compilation error i.e  lvalue required as increment operand. Lvalue required means you cannot assign a value to something that has no place in memory. Basically you need a variable to be able to assign a value.

For program 2:

(a) First two statments are valid statments:

int x=foo(); -> foo() method simply returns the return value i.e 2 and since the value is getting assignes in an integer only hence it will give output as 2.

x++; -> Since in the above statment the x is getting value 2 now this statement will increment the value of x by 1 and hence the ouput is 3

Third statement is invalid

foo()++ -> This will give you a compilation error as: lvalue required as increment operand. Because you can not apply increment operator on a function.

(b) First two are valid

const int * p1 = bar(); -> It will point to the base address of the array a. Hence will give output as 1

p1++ ; -> Since we are incrementing the base address, so now it will point to the second element of the array and will give output as 2.

Third, fourth and fifth are invalid:

(*p1)++ -> Since this is pointer to a constant hence you can not modify the value of p1 while you can modify the base address. Hence results into compilation error.

bar()++ -> This will give you a compilation error as: lvalue required as increment operand. Because you can not apply increment operator on a function.

(*bar())++ -> Since the function returns an constant value, hence it can not be modified and gives a compilation error as increment of read-only location '* bar()

(c) First, second and fifth are valid

const int * p2 = baz(); ->  It will point to the base address of the array a. Hence will give output as 1

p2++ ; -> Since we are incrementing the base address, so now it will point to the second element of the array and will give output as 2.

(*baz())++ -> Since this declares a constant pointer to int type, you are allowed to modify baz() but not object pointed by baz(). Hence the ouput is 2

Third and fourth are invalid:

(*p2)++ -> Since this declares a constant pointer to int type and the function returns a constant value you can not modify it. Hence it results into a compilation error as increment of read-only location '* p2'

baz()++ -> This will give you a compilation error as: lvalue required as increment operand. Because you can not apply increment operator on a function.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote