代码:- 通过交换来反转数组的元素
编译器:- 代码块 13.12
每当我尝试编译和运行此代码时,都会出现一个未知值而不是反向数组,调试后我意识到代码在交换部分之前工作得很好,真正的问题在那之后开始,同时打印 c 的值分配为零的值会自动分配给我在程序开始时为 n 设置的值;
因此它打印出 arr[n] 的值,该值还没有被我分配,因为它是一个垃圾值
我在网上编译了一下,同样的问题。请帮助我,我很好奇想找出我的错。
#include<stdio.h>
int main()
{
int n,c,t,end,arr[100];
//entering the number of elements
printf("enter the number of elements of array\n");
scanf("%d",&n);
end=n-1;
//printing the array
printf("enter your array\n");
for (c=0;c<n;c++)
scanf("%d",&arr[c]);
//swapping the array's value
for (c=0;c<n/2;c++)
{
t=arr[c];
arr[c]=arr[end];
arr[end]=t;
end--;
}
//printing the new array
printf("reversed array is\n");
for (c= 0;c<n;c++);
/*at this point the value of c becomes equal to the n (i.e, the number of
elements of the array)*/
printf("%d\n",arr[c]);
return 0;
}
最佳答案
最后一个 for 循环已结束; 删除它们
关于c - 即使在调试我的程序时也无法弄清楚整数值为何发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33680882/