c - 即使在调试我的程序时也无法弄清楚整数值为何发生变化?

标签 c arrays

代码:- 通过交换来反转数组的元素

编译器:- 代码块 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/

相关文章:

c++ - 具有 uint8 数组奇怪行为的 C 结构

C错误: called object is not a function or function pointer

c++ - 麻省理工学院球体模拟设置中的 Matlab 错误

c++ - FormatMessage 中是否有明确定义的大小限制?

php - 如何将 ARRAY 作为键值对添加到关联数组

php - 如何修剪数组中的所有字符串?

arrays - 使用字符串数组作为值在swift中创建字典

ios - 如何在不打开 Xcode 的情况下直接 React native 应用程序?

c - 函数声明符代码块之后的预期函数体

c - C语言中 "int arr[] = {}"和 "int arr[]"的区别