【发布时间】:2017-04-18 02:16:42
【问题描述】:
我在多个帖子中看到过这个问题,但我还没有找到一个对我有很好解释的问题。我试图创建一个链表,但是如果没有得到错误就不能调用结构和函数,不能将其转换为指针。它真的困扰着我。任何有关如何使其正常工作的帮助将不胜感激。下面的一些代码就是问题所在。
typedef struct node
{
void *data;
struct node *next;
} node;
node *head = NULL;
node* create(void *data, node *next)
{
node *new_node = (node*)malloc(sizeof(node));
if(new_node == NULL)
{
exit(0);
}else{
new_node->data = data;
new_node->next = next;
return new_node;
}
}
node* prepend(node *head, void *data)
{
node *new_node = create(data,head);
head = new_node;
return head;
}
void preload_adz(int adz_fd)
{
struct adz adz_info;
char adz_data[40];
char adz_text[38];
int adz_delay;
char adz_delayS[2];
read(adz_fd,adz_data,40);
strncpy(adz_text,adz_data + 2,40-2);
sprintf(adz_delayS, "%c%c",adz_data[0],adz_data[1]);
adz_delay = atoi(adz_delayS);
adz_info.delay = adz_delay;
strncpy(adz_info.text,adz_text,38);
head = prepend(head, (void*)adz_info); //<---This line throws the error
while(read(adz_fd,adz_data,40) > 0)
{
}
}
【问题讨论】:
-
您不能将结构转换为指针。您究竟想在那里实现什么目标?
-
我正在尝试启动链表,正在学习一些教程,这正是教程中的做法:/ zentut.com/c-tutorial/c-linked-list/#C_Linked_List_Program
-
(void*)adz_info-->&adz_info? -
@BLUEPIXY 这就是问题所在。哈哈。我傻了。
-
你好
exit(0)当 malloc 严重失败时