Dynamically allocate memory for Array of Structs

前端 未结 5 1674
北恋
北恋 2020-12-11 09:34

Here\'s what I\'m trying to do:

#include 
#include 

struct myStruct {
    int myVar;
}

struct myStruct myBigList = null;

vo         


        
5条回答
  •  無奈伤痛
    2020-12-11 10:01

    This is because struct name is not automatically converted into a type name. In C (not C++) you have to explicitly typedef a type name.

    Either use

    struct myStruct instance;
    

    when using the type name OR typedef it like this

    typedef struct {
        int myVar;
    } myStruct;
    

    now myStruct can simply be used as a type name similar to int or any other type.

    Note that this is only needed in C. C++ automatically typedefs each struct / class name.

    A good convention when extending this to structs containing pointers to the same type is here

提交回复
热议问题