Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何将结构中的空指针指定给另一个结构?_C_Data Structures_Void Pointers_Doubly Linked List - Fatal编程技术网

C 如何将结构中的空指针指定给另一个结构?

C 如何将结构中的空指针指定给另一个结构?,c,data-structures,void-pointers,doubly-linked-list,C,Data Structures,Void Pointers,Doubly Linked List,我需要一些使用双链接列表的帮助,其中节点的结构包含指向void的指针。如果我定义了另一个结构,希望在其中插入节点的实际数据,那么如何将其分配给指向void的指针?另外,如何打印列表 在头文件中定义的我的节点的结构: typedef struct nodetype { struct nodetype *prev, *next; void *data; } NodeT; 要插入到每个节点中的数据结构,在main.c中定义: typedef struct dataStructure

我需要一些使用双链接列表的帮助,其中节点的结构包含指向void的指针。如果我定义了另一个结构,希望在其中插入节点的实际数据,那么如何将其分配给指向void的指针?另外,如何打印列表

在头文件中定义的我的节点的结构:

typedef struct nodetype
{
    struct nodetype *prev, *next;
    void *data;
} NodeT;
要插入到每个节点中的数据结构,在main.c中定义:

typedef struct dataStructure
{
    int birthday;
    char *name;
}

您需要正确定义第二个typedef,例如

typedef struct dataStructure
{
    int birthday;
    char *name;
} dataStructure;
然后,一旦分配/定义了结构,就可以将第一种类型中的指针设置为第二种类型,就像正常情况一样:

dataStructure mydatastruct;
node->data = &mydatastruct;  //(void *) can point to anything

如果希望通过节点结构访问数据结构的成员,则需要将void指针强制转换为
(dataStructure*)
,例如

((dataStructure *)node->data)->birthday = 1980;
宏可以帮助减少这种情况


如果您的节点结构只指向该数据结构,那么直接指向它会比使用空指针简单得多。

您用于学习如何管理双=链表指针的参考资料中没有关于如何分配内存或分配给指针的内容?烧掉那本书。旁注:您的
数据结构
类型定义不合法。没有要与typedef关联的结束名。但是,
nodetype
看起来是正确的。我忘记在数据结构定义中添加“struct”。我得到的参考资料很短,不全面。。
((dataStructure *)node->data)->birthday = 1980;