1

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?

2
  • 1
    You are probably attempting to write into a memory address which does not have Write access permission. This memory address happens to be 0 (the offset of the node_type field from the beginning of the node structure that is supposed to be allocated at address 0). Did you have any specific reason to set the node1 variable to point to that memory address (NULL)? Commented Dec 17, 2014 at 18:31
  • NULL is a sentinel value which indicates an invalid pointer. So you explicitly create an invalid pointer... and then proceed to use it. Commented Dec 17, 2014 at 18:36

1 Answer 1

1

You have to allocate memory for the node. For example

node_p *node1 = malloc( sizeof( node_p ) );
if ( node1 != NULL )
{
    node1->node_type = FRUIT;
    node1->data.question = "Apple";
}

And do not forget to free the allocated mempry then the node will not be needed any more using function free:

free( node1 );
Sign up to request clarification or add additional context in comments.

8 Comments

Your answer implies that OP must allocate this memory dynamically. This is not mandatory. As long as the node1 variable is set to point to a large enough memory space dedicated to a node object, OP's code is good. For example, a pre-allocated node object on the stack, or a large enough array (in which case, address alignment is also critical for success).
@barak manos If code would be good he never gets the segmentation fault. So your remark has no sense because OP uses a null pointer.
What kind of answer is that? Did you skip the "As long as" prefix at the beginning of the sentence??? BTW, I was merely trying to suggest that you should not make OP think that malloc was the only way. Oh and hmmm... I'm not the one who just took off your up-vote here, so please don't go around down-voting random posts of mine...
@barak manos I think If he has a question he can ask it himself.
OP asks why his code isn't working, when clearly the pointer is set to NULL and then used in order to access the pointed memory. It's not like he forgot to initialize it. He clearly doesn't understand the idea in using pointers. I think that a deeper explanation would be a little more informative in this case.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.