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