文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C简单实现动态2维数组

C简单实现动态2维数组

时间:2007-06-12  来源:白石

这也是近期编写 libscws 时用到的一个小功能,动态2维数组。

struct xxx **x;
x = (struct xxx **) darray_new(5, 4, sizeof(struct xxx));

...
这里就可以用 x[1][3]  ... x[0][3] ... 来操作了:)
...

darray_free(x);

CODE:
[Copy to clipboard]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void **darray_new(int row, int col, int size)
{       
        void **arr;

        arr = (void **) malloc(sizeof(void *) * row + size * row * col);
        if (arr != NULL)
        {
                void *head;

                head = (void *) arr + sizeof(void *) * row;
                memset(arr, 0, sizeof(void *) * row + size * row * col);
                while (row--)               
                        arr[row] = head + size * row * col;               
        }
        return arr;
}

void darray_free(void **arr)
{
        if (arr != NULL)
                free(arr);       
}

__________________________________
相关阅读 更多 +
排行榜 更多 +
憨豆快递(special delivery)

憨豆快递(special delivery)

休闲益智 下载
特技摩托挑战(wheelie challenge)

特技摩托挑战(wheelie challenge)

赛车竞速 下载
创造世界游戏

创造世界游戏

冒险解谜 下载