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??

NUO
  • 247
  • 2
  • 3
  • 14
  • 1
    `ptr = malloc(sizeof(tree_ptr));` -->> `ptr = malloc(sizeof *ptr);` oh, the joy of typedefs ... happy happy joy joy joy ... – wildplasser Feb 13 '16 at 15:41
  • @wildplasser sorry, it still can't work . – NUO Feb 13 '16 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 `malloc`ed memory segment and `t -> head` is still pointing to `NULL`. – Spikatrix Feb 13 '16 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. – lurker Feb 13 '16 at 15:50
  • Do **not** `typedef` pointer! – too honest for this site Feb 13 '16 at 16:20
  • @Olaf I wish I could, part of codes,like definition of struct are given to me. i could do nothing about that. – NUO Feb 13 '16 at 18:42

1 Answers1

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 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. – NUO Feb 13 '16 at 18:47
  • 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.http://stackoverflow.com/questions/35384096/a-nested-struct-with-pointers – NUO Feb 13 '16 at 19:05