hurriedly% 2018-11-11 12:24 采纳率: 100%
浏览 305
已采纳

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

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

  • lrony* 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.

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

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试