用宏实现动态数组

    提示:以下代码均未经详细测试。如果哪位有空,提出点意见,不胜感激,另本主题是用宏实现,所以不相关的就不要多说了。

#ifndef _VECTOR_H_
#define _VECTOR_H_

#include

typedef struct _vector{
    int size;
    void *data;
}VECTOR,*PVECTOR;

#define vector_init(x,t,z) do{/
                           PVECTOR v;int n;/
                           v=(PVECTOR)(x);n=(int)(z);/
                           v->data=malloc(v->size=sizeof(t)*n);/                         
                           }while(0)
                         
#define vector_cleanup(x) free((x)->data)
                         
#define vector_get(v,t,n) (((t*)((v)->data))[n])

#define vector_set_fast(v,t,n,d) (((t*)((v)->data))[n]=(d))

#define vector_set(x,t,z,d) do{/
                            PVECTOR v;int n;t temp;/
                            v=(PVECTOR)(x);n=(int)(z);temp=(d)/
                            if(n*sizeof(t)>v->size)/
                            {/
                                  void *dt;
                                dt=realloc(v->size=n*sizeof(t));/
                                if(!dt) break;/
                                v->data=dt;/
                            }/
                            memcpy(((t*)v->data)+n,&temp,sizeof(t));/
                            }while(0)
                               

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值