0

I am using a data structure to implement a spellchecking. I had two struct, node and table, which are defined in the following:

#include <stdlib.h>
typedef struct node *tree_ptr;
typedef struct table * Table;
struct node
{
    char* element;
    tree_ptr left, right;
};

typedef struct table
{
    tree_ptr head;
    int tree_h;
}table;

int main() {
    Table t = malloc(sizeof(table));
    t->head = NULL;
    tree_ptr ptr = t->head;
    ptr = malloc(sizeof(tree_ptr));
    ptr->element = "one";
    ptr->left = NULL;
    ptr->right = NULL;
    printf("%s\n",t->head->element);
   return 0;
} 

This programme has bug in the last line of print function, since t->head is pointing to NULL.

As I know, when changing a pointer's content value, the variable which the pointer points to is automatically changed.

Since t->head and ptr are both pointers, and ptr points to the t->head, that's, they are pointing to the same object.

Then when I change the ptr's value, why t->head doesn't change in the same way?? What should I do to achieve that t->head changes as ptr changes??

6
  • 1
    ptr = malloc(sizeof(tree_ptr)); -->> ptr = malloc(sizeof *ptr); oh, the joy of typedefs ... happy happy joy joy joy ... Commented Feb 13, 2016 at 15:41
  • @wildplasser sorry, it still can't work . Commented Feb 13, 2016 at 15:45
  • "Then when I change the ptr's value, why t->head doesn't change in the same way?" -- Because ptr is made to point to the malloced memory segment and t -> head is still pointing to NULL. Commented Feb 13, 2016 at 15:48
  • Once you write ptr = malloc... then ptr is no longer t->head. And you never set t->head to anything but NULL, so it remains NULL. Commented Feb 13, 2016 at 15:50
  • Do not typedef pointer! Commented Feb 13, 2016 at 16:20

1 Answer 1

2

You have to assign ptr back to t->head. Apart from this you have to allocate sizeof(struct node) for one node:

int main() {
    Table t = malloc(sizeof(table));
    t->head = NULL;

    tree_ptr ptr = malloc( sizeof(struct node) );
                              //         ^^^^      
    ptr->element = "one";
    ptr->left = NULL;
    ptr->right = NULL;

    t->head = ptr; // <-------

    printf("%s\n",t->head->element);
   return 0;
} 

Note ptr = t->head only assigns the value of t->head to ptr. ptr = malloc(....) allocates dynamic memory and assigns the address of the memory to ptr and overwrite the value of t->head which was there before. But the address of the memory is never assigned to t->head. There is no magical linkage between ptr and t->head.

What you tried to do is somthing like this:

tree_ptr *ptr = &(t->head);
*ptr = malloc( sizeof(struct node) );
(*ptr)->element = "one";
(*ptr)->left = NULL;
(*ptr)->right = NULL

In this case ptris a pointer to t->head and *ptr = malloc( sizeof(struct node) ) assigns the address of the allocated memory where ptr refers to and that is t->head.

Sign up to request clarification or add additional context in comments.

2 Comments

things are a bit tricky here. All the definition code is given, I could do nothing about that. What I want to implement is a table which contains a complete ordered tree, where the tree's head is stored. If you implement ptr as a pointer to t->head, then, what if I want to use the ptr to go over the tree( like inserting a new node, I need to go over the tree as well as make new changes to the tree) , the t->head will be changed as well, then I will lose the head.
I knew why this doesn't work but I don't know how to solve it. I raised the further question in the following, hope you can help me. Thanks.stackoverflow.com/questions/35384096/…

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.