char data[0]在struct末尾的用法

今天在看一段代码时出现了用结构体实现变长数组的写法,一开始因为忘记了这种技术,所以老觉得作者的源码有误,最后经过我深思之后,终于想起以前看过的用struct实现变长数组的技术。下面是我在网上找到的一篇讲解很清楚的文章。

在实际的编程中,我们经常需要使用变长数组,但是C语言并不支持变长的数组。此时,我们可以使用结构体的方法实现C语言变长数组。

struct MyData { int nLen; char data[0];};
在结构中,data是一个数组名;但该数组没有元素;该数组的真实地址紧随结构体MyData之后,而这个地址就是结构体后面数据的地址(如果给这个结构体分配的内容大于这个结构体实际大小,后面多余的部分就是这个data的内容);这种声明方法可以巧妙的实现C语言里的数组扩展
实际用时采取这样:
struct MyData *p = (struct MyData *)malloc(sizeof(struct MyData )+strlen(str))

这样就可以通过p->data 来操作这个str。

程序实例:

    #include <iostream>  
    using namespace std;  
    struct MyData   
    {  
     int nLen;  
     char data[0];  
    };  
    int main()  
    {  
     int nLen = 10;  
     char str[10] = "123456789";  
     cout << "Size of MyData: " << sizeof(MyData) << endl;  
     MyData *myData = (MyData*)malloc(sizeof(MyData) + 10);  
     memcpy(myData->data, str, 10);  
     cout << "myData's Data is: " << myData->data << endl;  
     free(myData);  
     return 0;  
    }  

输出:
Size of MyData: 4

myData"s Data is: 123456789

原文链接:http://www.2cto.com/kf/201312/261179.html

PS:必须将指针定义在struct的末尾,指针的类型也可以不为char。

总结:

柔性数组
它只能放在结构体末尾,
是申明一个长度为0的数组(eg. char data[0] ;int data[0];都行)
可以使得这个结构体是可变长的。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <stdio.h> #include <stdlib.h> struct link { int data; struct link *next; }; struct link *AppendNode(struct link *head,int data); void DisplyNode(struct link *head); void DeleteMemory(struct link *head); struct link * DeleteNode(struct link *head,int data); struct link *InsertNode(struct link *head, int nodeData); int main() { char c; int data = 0; struct link head = NULL; / 链表头指针 / while (1) { scanf("%d",&data); if (data==-1) break; head = AppendNode(head,data);/ 向head为头指针的链表末尾添加节点 / } DisplyNode(head); / 显示当前链表中的各节点信息 / scanf("%d",&data); head = InsertNode(head,data); DisplyNode(head); / 显示当前链表中的各节点信息 / DeleteMemory(head); / 释放所有动态分配的内存 */ return 0; } struct link *AppendNode(struct link *head,int data){ struct link pnew,ptail; pnew=head; if(head==NULL){ head=(struct link)malloc(sizeof(struct link)); head->data=data; head->next=NULL; }else{ while(pnew!=NULL){ ptail=pnew; pnew=pnew->next; } pnew=(struct link)malloc(sizeof(struct link)); pnew->data=data; ptail->next=pnew; pnew->next=NULL; } return head; }; void DisplyNode(struct link *head){ struct link *p; p=head; while(p!=NULL){ if(p->next !=NULL) printf("%d->",p->data); else{ printf("%d\n",p->data); break; } p=p->next; } } struct link *InsertNode(struct link *head, int nodeData){ struct link *p,q,t; p=head; if(head->data>nodeData){ head=(struct link)malloc(sizeof(struct link)); head->data=nodeData; head->next=p; }else{ while(1){ if(p->next->data>nodeData) { q=(struct link)malloc(sizeof(struct link)); t=p->next; p->next=q; q->data=nodeData; q->next=t; break; } p=p->next; } } return head; }; void DeleteMemory(struct link *head) { free (head); }
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值