简体   繁体   English

在 C 数组中编程

[英]programming in c array

I'm reading Programming in C by Kochan, 3rd ed.我正在阅读 Kochan 第 3 版的《C 语言编程》。

In the introduction to arrays (program 7.1) he gives an example:在数组介绍(程序 7.1)中,他举了一个例子:

#include <stdio.h>


int main(void)
{
    int values[10];
    int index;

    values[0] = 197;
    values[2] = -100;
    values[5] = 350;
    values[3] = values[0] + values[5];
    values[9] =
    values[5] / 10;
    --values[2];

    for ( index = 0; index < 10; ++index )
        printf ("values[%i] = %i\n", index, values[index]);


    return 0;
}

which should give output这应该给出输出

values[0] = 197
values[1] = 0
values[2] = -101
values[3] = 547
values[4] = 0
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35

And with my quadriple-checking that i'm using the exact same code it only works halfway and gives me some weird extra numbers a [1], [4]通过我的四重检查,我使用的是完全相同的代码,它只能工作一半,并给我一些奇怪的额外数字 [1], [4]

values[0] = 197
values[1] = 3210052
values[2] = -101
values[3] = 547
values[4] = 17704192
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35

I'm using cl arrays.c -o arrays to compile.我正在使用cl arrays.c -o arrays进行编译。

Local variables are not initialized.局部变量没有被初始化。 That means the entries in the array that you do not explicitly initialize will contain indeterminate values (values that will be seen as random).这意味着数组中未明确初始化的条目将包含不确定值(将被视为随机值)。

Actually, using uninitialized variables (or uninitialized entries of an array) is undefined behavior .实际上,使用未初始化的变量(或数组的未初始化条目)是未定义的行为

they're not initialized, so what you see in consolle output is what there was in the same memory locations它们未初始化,因此您在控制台输出中看到的是相同内存位置中的内容

you may wonder why the first example gives '0' for each uninitialized value and "wierd numbers" in your implementation....maybe the first example was allocated into a memory area never used before, or maybe some compiler decides to set at 0x0000000 every memory register referred but not initialized您可能想知道为什么第一个示例在您的实现中为每个未初始化的值和“奇怪的数字”提供 '0'....也许第一个示例被分配到以前从未使用过的内存区域,或者某些编译器决定设置为 0x0000000每个内存寄存器都被引用但未初始化

If you look at the cod provided in the book it never sets the value of array entries 1 or 4. They are therefore undefined and could be any value.如果您查看书中提供的 cod,它从不设置数组条目 1 或 4 的值。因此它们是未定义的,可以是任何值。 Some compilers may go ahead and initialize them to zero but this is not part of the C specification and should never be relied upon.一些编译器可能会继续将它们初始化为零,但这不是 C 规范的一部分,永远不应依赖。

I read the text that explain this program in the book which clearly says that (pg no. 100):我阅读了书中解释该程序的文字,其中清楚地说明了(第 100 页):

Because you never assigned values to five of the elements in the array—elements 1, 4, and 6 through 8—the values that are displayed for them are meaningless.因为您从未为数组中的五个元素(元素 1、4 和 6 到 8)赋值,所以为它们显示的值毫无意义。 Even though the program's output shows these values as zero, the value of any uninitialized variable or array element is not defined.即使程序的输出将这些值显示为零,也未定义任何未初始化的变量或数组元素的值。 For this reason, no assumption should ever be made as to the value of an uninitialized variable or array element.出于这个原因,永远不应假设未初始化的变量或数组元素的值。

And this is what standard says that the uninitialized automatic local variable invokes undefined behavior.这就是标准所说的未初始化的自动局部变量调用未定义的行为。

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

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