谁还没个明天 2018-11-11 12:24 采纳率: 50%
浏览 285
已采纳

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

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条回答 默认 最新

  • 游.程 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.

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

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程