前提是不使用临时数组暂存:
下面是通过指针整体移位:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // ptr指向数组的第一个元素
int j;
for(j=0;j<5;j++)
{
printf("%4d",ptr[j]);
}
printf("\n");
ptr = ptr - 1;
for(j=0;j<5;j++)
{
printf("%4d",ptr[j]);
}
}
输出结果是:
1 2 3 4 5
0 1 2 3 4
我希望把前3个数往后移一位,而最后的5不变:请问有啥好的办法?多谢
0 1 2 3 5
--
FROM 111.193.229.*