如何理解下标为0的数组?

wang_qiao_ying wang_qiao_ying 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
nclude <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct helloworld_t
{
int num;
char helloworld[0];
};

int main()
{
struct helloworld_t *p;
unsigned int size = sizeof(struct helloworld_t) + strlen("Hello World!\n") + 1;
p = (struct helloworld_t *) malloc(size);
assert(p!=NULL);

memcpy(p, "\x01\x00\x00\x00Hello World!\n", size);

while (p->num--)
{
printf(p->helloworld);
}

//printf("%d \n", sizeof(helloworld_t));

free((void *)p);
return 0;
}

这个程序能正常打印hello world!,请高人帮忙理解一下char helloworld[0]的含义?


如果不在结构中定义下标为0的数组会报错,这是可以理解的,可是在结构中定义就没事了,这是为什么呢?
...全文

相关推荐

评论 13
收藏 已收藏
分享
返回顶部
wang_qiao_ying
C语言 70,040 成员 243,237 内容