C 带嵌套指针的嵌套结构

C 带嵌套指针的嵌套结构,c,pointers,struct,pointer-to-pointer,C,Pointers,Struct,Pointer To Pointer,我正在使用数据结构来实现拼写检查。我有两个结构,节点和表,它们定义如下: #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; }tabl

我正在使用数据结构来实现拼写检查。我有两个结构,节点和表,它们定义如下:

#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;
} 
#包括
typedef结构节点*tree_ptr;
typedef结构表*table;
结构节点
{
字符*元素;
树左,右;
};
typedef结构表
{
树头;
int tree_h;
}表;
int main(){
表t=malloc(sizeof(表));
t->head=NULL;
树\u ptr ptr=t->head;
ptr=malloc(sizeof(tree_ptr));
ptr->element=“一”;
ptr->left=NULL;
ptr->right=NULL;
printf(“%s\n”,t->head->element);
返回0;
} 
该程序在打印函数的最后一行有错误,因为t->head指向NULL

我知道,当更改指针的内容值时,指针指向的变量会自动更改

因为t->head和ptr都是指针,ptr指向t->head,也就是说,它们指向同一个对象


那么当我改变ptr的值时,为什么t->head没有以同样的方式改变??我应该怎么做才能使t->head随着ptr的变化而变化???

您必须将
ptr
分配回
t->head
。除此之外,您还必须为一个节点分配
sizeof(struct 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;
} 

在这种情况下,
ptr
是一个指向
t->head
的指针,并且
*ptr=malloc(sizeof(struct node))
分配
ptr
引用的分配内存的地址,也就是
t->head

您必须将
ptr
分配回
t->head
。除此之外,您还必须为一个节点分配
sizeof(struct 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;
} 

在这种情况下,
ptr
是一个指向
t->head
的指针,并且
*ptr=malloc(sizeof(struct node))
分配
ptr
引用的分配内存的地址,即
t->head

ptr=malloc(sizeof(tree\ptr))-->
ptr=malloc(sizeof*ptr)哦,typedefs的乐趣。。。快乐…@wildplasser抱歉,它仍然无法工作。“那么当我更改ptr的值时,为什么t->head不会以相同的方式更改?”--因为
ptr
指向
malloc
ed内存段,
t->head
仍然指向
NULL
。一旦您写入
ptr=malloc…
,那么
ptr
就不再是
t->head
。而且您从未将
t->head
设置为除NULL以外的任何值,因此它将保持NULL。不要
typedef
指针<代码>ptr=malloc(大小(树的ptr))-->
ptr=malloc(sizeof*ptr)哦,typedefs的乐趣。。。快乐…@wildplasser抱歉,它仍然无法工作。“那么当我更改ptr的值时,为什么t->head不会以相同的方式更改?”--因为
ptr
指向
malloc
ed内存段,
t->head
仍然指向
NULL
。一旦您写入
ptr=malloc…
,那么
ptr
就不再是
t->head
。而且您从未将
t->head
设置为除NULL以外的任何值,因此它将保持NULL。不要
typedef
指针!这里的事情有点棘手。所有的定义代码都给出了,对此我无能为力。我想要实现的是一个包含完整有序树的表,树的头部存储在这个表中。如果将ptr实现为指向t->head的指针,那么,如果我想使用ptr遍历树(比如插入一个新节点,我需要遍历树并对树进行新的更改),t->head也将被更改,然后我将丢失head。我知道这为什么不起作用,但我不知道如何解决它。我在下面提出了进一步的问题,希望你能帮助我。谢谢。这里的事情有点棘手。所有的定义代码都给出了,对此我无能为力。我想要实现的是一个包含完整有序树的表,树的头部存储在这个表中。如果将ptr实现为指向t->head的指针,那么,如果我想使用ptr遍历树(比如插入一个新节点,我需要遍历树并对树进行新的更改),t->head也将被更改,然后我将丢失head。我知道这为什么不起作用,但我不知道如何解决它。我在下面提出了进一步的问题,希望你能帮助我。谢谢