本实例实现使用指针将数组中的元素逆置,并将结果输出。
#include
int inver(int* x, int n)
{
int* p, temp, * i, * j, m = (n - 1) / 2;
i = x;
j = x + n - 1;
p = x + m;
for (; i<= p; i++,j--)
{
temp = *i;
*i = *j;
*j = temp;
}
return 0;
}
int main(void)
{
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
printf("原始数组的元素为:\n");
for (int i = 0; i < 10; i++)
{
printf("%d\n", a[i]);
}
printf("\n\n--------------------------------------------------\n\n\n");
inver(a, 10);
printf("逆置数组后的元素为:\n");
for (int i = 0; i < 10; i++)
{
printf("%d\n", a[i]);
}
printf("\n\n--------------------------------------------------\n\n\n");
return 0;
}