简体   繁体   English

为什么此C程序的输出不正确

[英]Why output of this C program is not correct

I have written ac program like this and expect to get correct outputs. 我已经编写了这样的交流程序,希望能获得正确的输出。

 #include <stdio.h>
 #include <stdlib.h>

 int main()
 {
     int t;
     scanf("%d", &t);
     int a[t-1], b[t-1];
     //printf("%d\n",sizeof(a) );
     do{
     scanf("%d %d", &a[t-1], &b[t-1]);
     }while(--t);

    do{
         printf("%d\n",a[t-1] + b[t-1]);
    }while(--t);

    return 0;
 }

But I am getting random values.What is wrong with this program? 但是我得到的是随机值,这个程序怎么了? The attempt is made to enter number of tries, then enter values , then print the addition of those values. 尝试输入尝试次数,然后输入值,然后打印这些值的加法。

PS the array initialization is dynamic here.Is it valid?If not what should be the correct way? PS数组初始化在这里是动态的,是否有效?

When you declare an array of length 10 , the index is from 0 to 9 . 声明长度为10的数组时,索引为09 There is no index 10 for that array. 该数组没有索引10

In your code, your array is of size t-1 在您的代码中,数组的大小为t-1

int a[t-1], b[t-1];

So, in your first iteration, the scanf() and printf() statement accesses a[t-1] , which is not present. 因此,在您的第一次迭代中, scanf()printf()语句访问a[t-1] ,该参数不存在。 And, since its a do while loop, it is guaranteed to run atleast once. 而且,由于它是一个do while循环,因此可以保证至少运行一次。

Accessing array outside its bounds is Undefined behavior . 访问数组超出其边界是未定义的行为 So, as Cool Guy pointed out in the comments, anything can happen. 因此,正如Cool Guy在评论中指出的那样,任何事情都可能发生。

Between reading all values and printing all values, you never reset t to it's original value. 在读取所有值和打印所有值之间,您永远不会将t重置为其原始值。 You will need to do that, probably using another variable to save it's original value. 您可能需要执行此操作,可能使用另一个变量来保存其原始值。

The array size should be a constant, it cant be set at run time. 数组大小应为常数,不能在运行时设置。 For run time memory allocation you need to use pointer variable with malloc function. 对于运行时内存分配,您需要将指针变量与malloc函数一起使用。 So first of all you need to assign a constant value to variable t at time of declaration. 因此,首先您需要在声明时为变量t分配一个常量值。

Just a tip, always try to declare and initialize the variable before any statements. 提示,请务必在任何语句之前声明和初始化变量。 The array initialization should come before scanf, by doing this you can clearly see the problem ie you are allocating memory(some random) and then getting value from the user. 数组初始化应该在scanf之前进行,通过这样做,您可以清楚地看到问题,即您正在分配内存(随机地),然后从用户那里获得价值。 The compiler must be doing the similar thing, allocating some random size memory and then getting the value t, now this value t will only be used in your iterations and not for array allocation as arrays are allocated before with some rand size (what ever garbage value t is pointing to at time of its declaration). 编译器必须做类似的事情,分配一些随机大小的内存,然后获取值t,现在此值t将仅在您的迭代中使用,而不用于数组分配,因为数组之前以一定的rand大小分配(无论有什么垃圾t在声明时指向的值)。 Sometime the program will be displaying some random values and sometime it could crash on accessing a memory that's out of bound. 该程序有时会显示一些随机值,有时会在访问超出范围的内存时崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM