217k views
3 votes
Assume that ip, jp, and tp have all been declared to be pointers to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array and that jp has been initialized to point to an element in the second half of the array. Write some code that makes jp point to the element that ip was pointing to and that makes ip point to the element that jp.Assume that ip, jp, and tp have all been declared to be pointers to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array and that jp has been initialized to point to an element in the second half of the array. Write some code that makes jp point to the element that ip was pointing to and that makes ip point to the element that jp.

User EldHasp
by
6.7k points

1 Answer

3 votes

Answer:

tp = ip;

ip = jp;

jp = tp;

Step-by-step explanation:

Here tp acts as a temporary and intermediary variable which is used to swap the pointers jp and ip. This is to save the original value of ip instead of over writing its values by jp. As ip has been initialized to point to an element in the first half of the array so tp now points to the element that ip has been initialized to. This way ip does not lose its original element pointing position. Next the statement ip = jp means that the ip now points to the element that jp was pointing to. Next statement jp = tp means that jp now points to the element that tp was pointing to. So what was tp pointing to? As the first statement tp = ip; tp points to the element that ip was pointing to so this jp = tp; statement means that now jp points to the element that ip was pointing to.

User Hugodby
by
7.4k points