c/c++语言开发共享c双指针数组

以下是我目前的代码。 我的教授告诉我们使用双指针来创建一个指针数组

struct dict { struct word **tbl; int (*hash_fcn)(struct word*); void (*add_fcn)(struct word*); void (*remove_fcn)(struct word*); void (*toString_fcn)(struct word*); }; struct word { char *s; struct word *next; }; 

struct dict * hashtbl;

部分主要function

  hashtbl=malloc(sizeof(struct dict)); hashtbl->tbl=malloc(sizeof(struct word)*256); int i; for(i=0;itbl[i]=NULL; } 

这是实现这种双指针数组的正确方法吗?

并正在使用

 hashtbl->tbl[i] = ..... 

访问该空间的正确方法?

    hashtbl->tbl=malloc(sizeof(struct word)*256);

    应该是

    hashtbl->tbl=malloc(sizeof(struct word *)*256);

    因为hashtbl->tbl是一个struct word *的数组

    要初始化struct word **tbl

     hashtbl->tbl = malloc(sizeof(struct word *)*256); if (hashtbl->tbl == NULL) { printf("Error malloc"); exit(1); } for (int i = 0; i < 256; i++) { hashtbl->tbl[i] = malloc(sizeof(struct word)); if (hashtbl->tbl[i] == NULL) { printf("Error malloc"); exit(1); } 

    要解除分配:

     for (int i = 0; i < 256; i++) { free(hashtbl->tbl[i]); } free(hashtbl->tbl); hashtbl->tbl = NULL; 

      以上就是c/c++开发分享c双指针数组相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/519685.html

      (0)
      上一篇 2020年12月5日
      下一篇 2020年12月5日

      精彩推荐