Allocating memory for a Structure in C

后端 未结 7 1534
清酒与你
清酒与你 2020-12-12 17:10

I\'m tasked to create a program which dynamically allocates memory for a structure. normally we would use

x=malloc(sizeof(int)*y);

However,

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-12 17:51

    My favorite:

    #include 
    
    struct st *x = malloc(sizeof *x); 
    

    Note that:

    • x must be a pointer
    • no cast is required
    • include appropriate header

提交回复
热议问题