gpt4 book ai didi

c - 在c中定义了一个空数组

转载 作者:行者123 更新时间:2023-12-04 12:33:38 25 4
gpt4 key购买 nike

在下面的程序中实际发生了什么,我在代码中和 GCC 编译器中定义了一个空数组 int arr[];,但是编译器没有给出错误。成功了。

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

typedef struct st
{
int i;
int arr[];
}ST,*ptr;

int main()
{
ST s1;
ptr p1= (ptr)malloc(sizeof(ST)+4*sizeof(int));

p1->i=10;
p1->arr[0]=1;
p1->arr[3] = 1;

printf("%d\n",p1->arr[3]);
printf("%ld\n", sizeof(s1));
}

C 语言不允许未定义的数组长度。但 GCC 编译器 允许。为什么?

只是好奇,到底发生了什么?

最佳答案

C99起有效并允许.它叫做flexible array member - 使结构的最后一个成员变长很有用,如果使用,它必须是结构的最后一个成员。

关于c - 在c中定义了一个空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39140478/

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