游.程 2018-11-11 12:24 采纳率: 0%
浏览 278
已采纳

如何正确计算一个结构的数组在另一个结构的空间?

I have two structs. I am trying the make an array of 'struct bird' inside another struct 'struct nest'.

I am having a hard time allocating the correct amount of space for the bird array when I am creating the nest struct.

Below is my code.

struct bird {
  int value;
};
typedef struct bird bird;

struct nest {
  int nb_birds;
  bird * * birds;     //bird * = points to the bird struct, * birds = Array with size unknown
};
typedef struct nest nest;

nest * create_nest(int nb_birds) {
  nest * n = (nest *) malloc(sizeof(nest));
  n->nb_birds = nb_birds;

   //This is where I am stuck
  ***n->birds = (bird *) malloc(sizeof(bird) * nb_birds);*** 


  int i;
  for(i = 0; i < nb_birds; i++)
    n->birds[i]=NULL;
  return n;
}

转载于:https://stackoverflow.com/questions/53248732/c-how-to-malloc-the-correct-amount-of-space-for-an-array-of-a-struct-inside-ano

  • 写回答

1条回答 默认 最新

  • local-host 2018-11-11 12:37
    关注

    You want to allocate array of nb_birds pointers to bird structure, so size to allocation is nb_birds * sizeof(bird *).

    Then you want to store pointer to this array, so cast should be to address of the first element — address of bird *, i.e. bird **.

    Hence,

    n->birds = (bird **) malloc(sizeof(bird *) * nb_birds);
    

    p.s. If you want to allocate N objects on which ptr points to, you can write, or, at least, think about as

    ptr = (typeof(ptr)) malloc(sizeof(*ptr) * N);
    

    Update:

    It should be noted that malloc returns void * pointer that compatible with any pointer type without explicit casting. So, quoted program line can be as short as

    ptr = malloc(N * sizeof(*ptr));
    

    Some programmers, despite them well informed about this void * property, strongly prefer to use explicit cast in such cases. I'm not one of them, but I account such casts as stylistіc preference (like () for sizeof operator). So I left the casting in code above because OP use it, and I thought it was his choice.

    Neverthless it is needed (at least for answer completeness and for further readers) to note that such cast is unnecessary and excessive. .

    Thank you Paul Ogilvie and chux for patient notes in the comments.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?