C - Setting a struct to null (incompatible types in assignment)

后端 未结 4 1840
旧时难觅i
旧时难觅i 2021-02-10 02:46

I have the following struct:

struct elem {
  int number;
  char character;
};

struct item {
  struct elem element;
};

and the following functi

相关标签:
4条回答
  • 2021-02-10 02:49
    1. element is not a pointer and hence cannot be assigned NULL.
    2. main->element is wrong usage. You cannot access a structure's element using it's name. It should be wrapper->element. You should access it using the variable name.
    0 讨论(0)
  • 2021-02-10 03:08

    In C NULL is generally defined as the following

    #define NULL ((void*)0)
    

    This means that it's a pointer value. In this case your attempting to assign a pointer (NULL) to a non-pointer value item::element and getting the appropriate message. It seems like your intent is to have element be a pointer here so try the following

    struct item {
      struct elem* element;
    };
    
    0 讨论(0)
  • 2021-02-10 03:13

    NULL is a pointer value, wrapper->element is not a pointer, therefore you cannot assign it NULL

    0 讨论(0)
  • 2021-02-10 03:13

    In addition to the previous answers, sometimes it makes sense to create a NULL struct. http://en.wikipedia.org/wiki/Null_Object_pattern

    0 讨论(0)
提交回复
热议问题