Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 a incompatible types in assignment. Why is that? Shouldn't setting a struct to NULL be okay?

like image 970
darksky Avatar asked Jul 10 '12 15:07

darksky


1 Answers

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;
};
like image 176
JaredPar Avatar answered Oct 07 '22 18:10

JaredPar