设为首页 加入收藏

TOP

c语言实现动态指针数组Dynamic arrays(二)
2015-01-22 21:02:36 来源: 作者: 【 】 浏览:107
Tags:语言 实现 动态 指针 Dynamic arrays
PE(DyArray); //if the input parameter is invalid, return. if(!pArr) { return NULL; } //malloc memory for dynamic array and iniatilize it. pArr->m_fDestroy = pDataDestroy; pArr->m_ppData = (void *)f_malloc(sizeof(void *)*MIN_PRE_ALLOCATE_SIZE); if(!pArr->m_ppData) { free(pArr); return NULL; } pArr->m_nAllocSize = MIN_PRE_ALLOCATE_SIZE; return pArr; } /************************************************************************************************** 【函数名】: DyArrayGetByIndex 【描述】:获取数组元素by index 【参数】: pArr: the array's address. nIndex: the element's position. ppData: out parameter, to record the element's pointer. 【返回值】: true or false. ***************************************************************************************************/ cp_bool DyArrayGetByIndex(DyArray* pArr, cp_int32 nIndex, void** ppData) { //if the input parameter is invalid, return. if(!pArr || nIndex < 0 || !ppData || nIndex >= pArr->m_nSize) { *ppData = NULL; return CP_FALSE; } *ppData = pArr->m_ppData[nIndex];//get the related element. return CP_TRUE; } /************************************************************************************************** 【函数名】: DyArrayGetFirst 【描述】:获取数组第一个元素 【参数】: pArr: the array's address. ppData: out parameter, to record the element's pointer. 【返回值】: true or false. ***************************************************************************************************/ cp_bool DyArrayGetFirst(DyArray* pArr,void** ppData) { return DyArrayGetByIndex(pArr,0,ppData) ? CP_TRUE : CP_FALSE; } /************************************************************************************************** 【函数名】: DyArrayGetLast 【描述】: 获取数组最后一个元素 【参数】: pArr: the array's address. ppData: out parameter, to record the element's pointer. 【返回值】: true or false. ***************************************************************************************************/ cp_bool DyArrayGetLast(DyArray* pArr,void** ppData) { return DyArrayGetByIndex(pArr,pArr->m_nSize-1,ppData) ? CP_TRUE : CP_FALSE; } /************************************************************************************************** 【函数名】:DyArraySetByIndex 【描述】: 设置数组元素by index 【参数】: pArr: the array's address. nIndex: the element's position. pData: the element's pointer. 【返回值】: true or false. ***************************************************************************************************/ cp_bool DyArraySetByIndex(DyArray* pArr, cp_int32 nIndex, void* pData) { //if the input parameter is invalid, return. if(!pArr || nIndex < 0) { return CP_FALSE; } pArr->m_ppData[nIndex] = pData;//find the related position and set its value. return CP_TRUE; } /************************************************************************************************** 【函数名】: DyArrayLength 【描述】:获取数组的长度 【参数】: pArr: the array's address. 【返回值】: 数组长度. ***************************************************************************************************/ cp_int32 DyArrayLength(DyArray* pArr) { return pArr ? pArr->m_nSize : -1; } /************************************************************************************************** 【函数名】: DyArrayFind 【描述】:查找数组中的指定元素 【参数】: pArr: the array's address. pCmp: the callback function to compare the data. pData: the searc
首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C和指针 (pointers on C)――第.. 下一篇C专家编程之编译器日期被破坏程序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: