c语言指针问题,大神帮帮忙
将数组a中n个整数按相反顺序存放,我写的代码如下:#include<stdio.h>
int main(void)
{
int inv(int *x, int n);
int i;
int a[10] = { 3, 7, 9, 11, 0, 6, 7, 5, 4, 2 };
printf("The original array:\n");
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("\n");
inv(a, 10);
printf("The array has been inverted;\n");
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("\n");
}
int inv(int *x, int n)
{
int temp,*i=*x , *j=*(x+n-1) , *m=*(x+(n-1)/2;
for (; i <= m; i++, j--)
{
temp = *i;
*i = *j;
*j = temp;
}
}
而正确代码是把inv函数的第一行改为int temp,*i,*j,*m;i=x;j=x+n-1;m=x+(n-1)/2;
我写的那块指针为什么是错误的,请帮忙解释一下,谢谢