如何理解下标为0的数组?
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的数组会报错,这是可以理解的,可是在结构中定义就没事了,这是为什么呢?
#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的数组会报错,这是可以理解的,可是在结构中定义就没事了,这是为什么呢?
...全文
相关推荐
PHP删除数组中指定下标的元素方法
下面是一个示例代码,展示了如何使用unset()函数删除指定下标的元素,并使用array_values()重新建立数组索引:```php<?
swift 学习笔记 之 下标脚本的使用
在Swift中,下标(subscript)是访问集合、数组或字典等可索引类型的快捷方式。这篇学习笔记将深入探讨Swift下标脚本的使用,帮助你更好地理解和应用这一特性。
C语言数组学习教案.pptx
二、数组的引用数组元素的引用需要使用下标,例如:`a[0]、a[1]、…、a[99]`。
C语言的二维数组的指针访问
三、理解运算符 *、&、[ ] 之间的关系在C语言中,*、&、[ ] 三个运算符之间存在着一定的关系。[ ] 是下标运算符,只适用于数组和指向数组的指针变量。
数组:为什么很多编程语言中数组都从0开始编号?.pdf
### 数组:为什么很多编程语言中数组都从0开始编号?#### 一、数组的基本概念数组是一种线性表数据结构,它使用一组连续的内存空间来存储一系列具有相同类型的数据。这一定义中有几个关键点:1.


