103k views
5 votes
The variables xp and yp have both been declared as pointers to integers, and have been assigned values. Write the code to exchange the two integers

User Noppadet
by
7.2k points

1 Answer

5 votes
The question is asking us to swap the values of xp and yp while not changing where they point to. Setting xp equal to yp would not work because then we couldn't change yp since the value for xp was overwritten. We can use a third variable to swap them.

int zp = xp;
xp = yp;
yp= zp;
User Tom Morgan
by
9.4k points