gpt4 book ai didi

c - C 中的指针跟踪

转载 作者:行者123 更新时间:2023-11-30 21:20:15 25 4
gpt4 key购买 nike

  • 以下代码片段的输出是什么?
  • <小时/>
    int arr[5] = {1, 2, 3};
    int *p1, *p2;
    p1 = arr;
    p2 = &arr[3];
    printf("%d\n", (*p1)++ + --(*p2));

    据我了解,第一行初始化一个包含 5 个内存空间的数组,仅填充位置 0,1 和 2。然后在第3行和第4行中,p1指向数组位置0,p2指向数组位置3,该位置为空。因此,在第 5 行,当问题尝试打印 (*p1)++ + --(*p2) 时,由于 p2 指向不包含任何值的内存,它将打印什么?

    最佳答案

    如果初始值设定项少于数组元素,则多余的数组元素将初始化为零。

    This answer引用了C99标准的相关部分:

    The C and C++ standards guarantee that even if an integer array is located on automatic storage and if there are fewer initializers in a brace-enclosed list then the uninitialized elements must be initialized to 0.

    C99 标准 6.7.8.21

    If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

    关于c - C 中的指针跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40727406/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com