未定义值的索引处的数组值

array value at an index for which value hasn't been defined

在此代码中:

#include <stdio.h>

int main(void) 
{
    int a[2][3] = {{1,2},{4,5,6}};

    printf("%d\n",a[0][2]);
    return 0;
}

输出为 0 - 由于数组未初始化,此输出是否是某些未定义行为的结果?

数组已初始化。您正在定义一个 two-dimensional 数组,该数组由两个数组组成,每个数组包含三个整数。对于这两个中的第一个,你只给出两个值,默认情况下,缺失的一个被初始化为零,所以你的完整数组是 {{1, 2, 0}, {4, 5, 6}。 a[0][2] 会给你那个零。

来自C标准(6.7.9初始化)

19 The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.

10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

所以实际上这个声明

int a[2][3] = {{1,2},{4,5,6}};

等同于

int a[2][3] = {{1,2, 0},{4,5,6}};