Suppose the following declarations and statement are m effec


Suppose the following declarations and statement are m effect What are the values of and b in each ease? int a = 10, b - 20, x = &a;, y = &b; x = y; int a - 10, b - 20, x =6a, y =&b; x = y; x = 2; y = 5; int a = 10, b = 20, x =&a;, y = &b; x = y; x + = 2; [y] --;

Solution

Note: x = &a means x is an integer pointer which should store the address of the variable a. & is the addressOf operator. By *x, we denote the value of the variable stored at the address x. * is the valueAt operator.

Answers

(a) a = 20, b = 20.

This is because, x and y are the pointers which point to the memory locations where a and b are stored, respectively.

When you set *x = *y, it means you are assigning the variable stored in the address denoted by x (which is a) to the value of the integer stored in the address denoted by the pointer y(which is b).

Hence, both a and b would have value 20, in the end.

(b) a = 2, b = 5.

This is because, x and y are the pointers which point to the memory locations where a and b are stored, respectively.

When you set *x = *y, it means you are assigning the variable stored in the address denoted by x (which is a) to the value of the integer stored in the address denoted by the pointer y(which is b). At this point, a = 20, b = 20.

After this, you have *x = 2, i.e. you are assigning the variable stored in the address denoted by the pointer x (which is a) to 2.

Then, *y = 5, i.e. you are assigning the variable stored in the address denoted by the pointer y (which is b) to 5.

(c) a = 22, b = 19.

This is because, x and y are the pointers which point to the memory locations where a and b are stored, respectively.

When you set *x = *y, it means you are assigning the variable stored in the address denoted by x (which is a) to the value of the integer stored in the address denoted by the pointer y(which is b). At this point, a = 20, b = 20.

After this, you have *x += 2, i.e. you instruct to increase the value of the variable stored in the the address denoted by x (which is a) by 2.

Finally, you have (*y)--, i.e. you instruct to decrease the value of the variable stored in the the address denoted by y (which is b) by 1.

 Suppose the following declarations and statement are m effect What are the values of and b in each ease? int a = 10, b - 20, x = &a;, y = &b; x = y; in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site