C - Setting a struct to null (incompatible types in assignment)
Solution 1
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;
};
Solution 2
NULL
is a pointer value, wrapper->element
is not a pointer, therefore you cannot assign it NULL
Solution 3
In addition to the previous answers, sometimes it makes sense to create a NULL struct. http://en.wikipedia.org/wiki/Null_Object_pattern
Solution 4
- element is not a pointer and hence cannot be assigned NULL.
main->element
is wrong usage. You cannot access a structure's element using it's name. It should bewrapper->element
. You should access it using the variable name.
darksky
C, C++, Linux, x86, Python Low latency systems Also: iOS (Objective-C, Cocoa Touch), Ruby, Ruby on Rails, Django, Flask, JavaScript, Java, Bash.
Updated on June 29, 2020Comments
-
darksky almost 4 years
I have the following struct:
struct elem { int number; char character; }; struct item { struct elem element; };
and the following function:
void init(struct item *wrapper) { assert(wrapper != NULL); wrapper->element = NULL; }
item->element = NULL
yields aincompatible types in assignment
. Why is that? Shouldn't setting a struct to NULL be okay?