0

Just a very brief question really which stemmed from another post, if I were to access my char *number within my ListNode, from my TreeNode, so that each TreeNode each has its own linked list of numbers, would I access it as follows ( where TreeNode *root):

root->name = strdup(name); root->numbers->number = strdup(number);

Cheers!

typedef struct ListNode {
  char            *number;
  struct ListNode *next;
}ListNode;

typedef struct TreeNode {
  char            *name;
  ListNode        *numbers;
  struct TreeNode *left;
  struct TreeNode *right;
}TreeNode;

EDIT: Here is my function to add a TreeNode and List to that Node:

int main(void) {
char my_string[50], name[25], number[25];
TreeNode *root = NULL;
ListNode *list = NULL;
while ((fgets(my_string, 50, stdin)) != NULL) {
    if (my_string[0] == '.')
        break;      
sscanf(my_string, "%s %s", name, number); 
root = AddNode(root, list, name, number);
}   
return 0;
}

TreeNode* AddNode(TreeNode *root, ListNode *list, char *name, char *number) {
int comparison;
if ( root == NULL) {
    root = (TreeNode *)malloc(sizeof(TreeNode));
    list = (ListNode *)malloc(sizeof(ListNode));
    root->name = strdup(name); root->numbers->number = strdup(number);
    root->left = root->right = NULL;

3 Answers 3

1

The problem is you're passing in a pointer to a ListNode, but then are mallocing a ListNode, assigning to it, and not doing anything with it. I'm not sure what your intent is there:

list = (ListNode *)malloc(sizeof(ListNode));

The reason for the segfault is that you malloc a TreeNode but not the ListNode inside of it. You need to do:

root = (TreeNode *)malloc(sizeof(TreeNode));
root->numbers = (ListNode *)malloc(sizeof(ListNode));

Or if that's what you meant to do with the ListNode you malloc'd, you need to assign it:

root = (TreeNode *)malloc(sizeof(TreeNode));
list = (ListNode *)malloc(sizeof(ListNode));
root->numbers = list;
Sign up to request clarification or add additional context in comments.

2 Comments

Similarly then, to how I set root->left and root->right to NULL, at the end of my code, would I also do the same with root->numbers->next, so that the next pointer of my list, for that TreeNode, points to NULL?
One thing you can do is use calloc() instead of malloc(). calloc() allocates the memory then zero-fills it, therefore any pointers you haven't assigned to will be NULL Edit: To clarify - Yes, you want root->numbers->next to be NULL
0

If you don't forget to malloc() that ListNode in there, that's the way to go!

5 Comments

But I seem to seg fault when I access in that way, any ideas? I am mallocing it, since atm I am only testing with root == NULL to begin with.
you should add that piece of info in the question.
Checking root as NULL? So when you start off building the tree, the call is AddNode(NULL, ....) - is that so?
In your AddNode, you should have a line like root->numbers = list
You're mallocating list, a local pointer and NOT the one inside root.
0

i think root->numbers->number is causing the segment violation, you do not seem to set root->numbers to point to anything before you de-reference it.

Comments

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.