0
Please someone should help explain exactly what this code does.
void shift(int* p){ int i,temp,*q=p+1; temp=*p; for(i=0;i<4;i++) { *p++=*q++; } *p=temp; }
1 Answer
+ 3
Ex:
If an array is passed to function, then for ex:
[1,2,3,4,5]
*q=p+1=> cause *q= 2 since *p points to 1 , first element of array..
temp=*p; // storing first element in temp = > 1
for(i=0;i<4;i++)
{
*p++=*q++; //this loop does all *q to *p means 2 in place of 1, *q=*q+1,*p =*p+1 increaments so now *q=3 and *p=2 since array is 2,2,3,4,5
In next iteration,
3 in place 2 , array is 2,3,3,4,5
In next iteration,
4 in place 3 , array is 2,3,4,4,5
In next iteration,
5 in place 4 , array is 2,3,4,5,5
Loop end now *p=5, temp=1
*p=temp;
Temp is assigned in *p so array is
2,3,4,5,1
Now array once shifted...
Hope it helps..