英文:
Can I declare an array in C without declaring its size and element?
问题
当我打印数组元素时,元素会是 hm 而非常数吗?
比如我输入 1、2、3 有时打印出来变成了 1、2、4 有时会出现另一个数字
``` C代码
#include <stdio.h>
int main()
{
int i, j=0;
int num[] = {};
for (i=0; i<=3; i++)
{
printf("Enter a number: ");
scanf("%d", &num[j]);
j++;
}
printf("\nnum1: %d\n", num[0]);
printf("num2: %d\n", num[1]);
printf("num3: %d\n", num[2]);
printf("num4: %d", num[3]);
}
这是我的代码和输出
Output
<details>
<summary>英文:</summary>
When I print the array element, the elements will be hm not constant?
Like if I input 1, 2, 3 sometimes when printed out it became 1, 2, 4 and sometimes another number just come up
#include <stdio.h>
int main()
{
int i, j=0;
int num[] = {};
for (i=0; i<=3; i++)
{
printf("\nEnter a number: ");
scanf("%d", &num[j]);
j++;
}
printf("\nnum1: %d\n", num[0]);
printf("num2: %d\n", num[1]);
printf("num3: %d\n", num[2]);
printf("num4: %d", num[3]);
}
This is my code and this is the output
[Output](https://i.stack.imgur.com/LhdGR.png)
</details>
# 答案1
**得分**: 1
当一个数组用`[]`定义维度并提供初始化时,其大小将从初始化中推断出。然而,`= {}`没有初始化。这不符合C语言的正式语法规定,规定括号内应至少有一个初始化项(C 2018 6.7.9 1)。因此,C标准未定义其行为,除非符合标准的编译器必须生成诊断消息。
一些编译器接受这种声明并将其视为零长度的数组。这是C标准未定义其行为的另一个原因,因为标准将数组定义为非空对象集合(C 2018 6.2.5 20)。
然后,程序试图将值放入`num[0]`、`num[1]`、`num[2]`和`num[3]`中。由于数组长度为零,这些元素在C计算模型中不存在,并且没有为它们保留内存。`scanf`调用可能会将这些值写入程序用于其他目的的内存中。由于程序正在使用该内存进行其他用途,它可能会更改该内存,导致稍后访问`num[0]`、`num[1]`、`num[2]`或`num[3]`的值与`scanf`写入的值不同。
> 我可以在C中声明一个没有声明大小和元素的数组吗?
您可以在不指定大小的情况下**声明**一个数组。在文件范围内,即在任何函数之外,`int num[];`将是一个有效的声明,告诉编译器可能会在其他地方定义一个名为`num`的数组。
当您**定义**一个数组时,应该指定其大小,可以通过在`[]`内明确给出大小(如`int num[3];`)或者提供初始值,从中编译器可以推断出大小(如`int num[] = { 9, 8, 7, 6 };`)。在函数体内,没有`extern`的数组声明也是一个定义,因此它应该包含大小。
<details>
<summary>英文:</summary>
When an array is defined with `[]` for the dimension and an initialization is given, its size is deduced from the initializers. However, `= {}` has no initializers. This does not conform to the formal grammar in C, which specifies there shall be at least one initializer inside the braces (C 2018 6.7.9 1). Because of this, the behavior is not defined by the C standard, except that a conforming compiler must produce a diagnostic message.
Some compilers accept this declaration and treat it as an array of zero length. This is another reason the behavior is not defined by the C standard, as the standard defines an array to be a nonempty set of objects (C 2018 6.2.5 20).
The program then goes on to attempt to put values in `num[0]`, `num[1]`, `num[2]`, and `num[3]`. Since the array has zero length, these elements do not exist in the C computing model, and no memory has been reserved for them. The `scanf` calls likely write these values into memory that the program is using for other purposes. And, since the program is using that memory for other purposes, it may change that memory, resulting in the values later accessed by `num[0]`, `num[1]`, `num[2]`, or `num[3]` to be different from what `scanf` wrote there.
> Can I declare an array in C without declaring its size and element?
You can **declare** an array without specifying its size. At file scope, outside of any function, `int num[];` would be a valid declaration that tells the compiler an array named `num` may be defined somewhere else.
When you **define** an array, you should specify its size, either by explicitly giving the size inside `[]` (as with `int num[3];`) or by giving initial values from which the compiler can deduce the size (as with `int num[] = { 9, 8, 7, 6 };`). Inside the body of a function, a declaration of an array without `extern` is also a definition, so it should include the size.
</details>
# 答案2
**得分**: 0
Declaration of an array without specified size in not valid in C language, as here compiler may be allocating an array of default size. And this is leading to the undefined behaviour of this program.
Also in this program stack smashing is hapenning, which is a kind of buffer overflow, which in case program is trying to write data beyond the allocated limit.
<details>
<summary>英文:</summary>
Declaration of an array without specified size in not valid in C language, as here compiler may be allocating an array of default size. And this is leading to the undefined behaviour of this program.
Also in this program stack smashing is hapenning, which is a kind of buffer overflow, which in case program is trying to write data beyond the allocated limit.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论