I cannot solve the following issue:
I have a struct like:
enum node_type {
FRUIT,
QUESTION
};
typedef enum node_type type;
struct node {
type node_type;
union node_info {
char *fruit;
char *question;
}data;
struct node *left;
struct node *right;
};
typedef struct node node_p;
When i try to access the member type (which is an enum), i can't change its value. It compiles, but when i run it i get a 'Segmentation Fault'. In my main method i have sth like this:
node_p *node1 = NULL;
node1->node_type = FRUIT;
node1->data.question = "Apple";
Does anyone know what the problem seems to be?
node_typefield from the beginning of thenodestructure that is supposed to be allocated at address 0). Did you have any specific reason to set thenode1variable to point to that memory address (NULL)?NULLis a sentinel value which indicates an invalid pointer. So you explicitly create an invalid pointer... and then proceed to use it.