[英]printing linked list with Node contains void pointer
Im a beginner in c and i need to print a linked list contents but the printing function doesnt work and i dont know why.我是 c 的初学者,我需要打印链表内容,但打印 function 不起作用,我不知道为什么。 i checked if the function of creating the linked list works by printf some words after using addFirst function and it seems the addFirst function works and the linked list has been created but the printing function doesnt Here is my code
i checked if the function of creating the linked list works by printf some words after using addFirst function and it seems the addFirst function works and the linked list has been created but the printing function doesnt Here is my code
#include <stdio.h>
#include <stdlib.h>
typedef enum msgtag {
NoTag = 0, Important, Work, Personal, ToDo, Later
} msgtag;
typedef struct MsgID
{
char* id ;
} MsgID;
typedef struct MsgDetails
{
char* id;
msgtag tag;
int year;
int month;
int day;
} MsgDetails;
typedef struct Node
{
void *item;
struct Node* next;
} Node;
Node* addFirst(MsgDetails*ptr,Node* head) {
Node* newHead = ( Node*)malloc(sizeof(Node));
newHead->item = malloc(sizeof(MsgDetails));
((MsgDetails*)newHead->item)->year = ptr->year;
((MsgDetails*)newHead->item)->day = ptr->day;
((MsgDetails*)newHead->item)->month = ptr->month;
((MsgDetails*)newHead->item)->id = ptr->id;
((MsgDetails*)newHead->item)->tag = ptr->tag;
newHead->next = head;
return newHead;
}
void printAll(Node* head) {
Node* current = NULL;
((MsgDetails*)current->item)->year = ((MsgDetails*)head->item)->year;
((MsgDetails*)current->item)->day = ((MsgDetails*)head->item)->day;
((MsgDetails*)current->item)->month = ((MsgDetails*)head->item)->month;
((MsgDetails*)current->item)->id = ((MsgDetails*)head->item)->id;
((MsgDetails*)current->item)->tag = ((MsgDetails*)head->item)->tag;
while (current != NULL) {
printf("%d,%d,%d,%d,%s",((MsgDetails*)current->item)->tag,((MsgDetails*)current->item)->year,((MsgDetails*)current->item)->month,((MsgDetails*)current->item)->day,((MsgDetails*)current->item)->id);
current = current->next;
}
printf("\n");
}
int main()
{
Node* head = NULL;
MsgDetails details;
MsgDetails *ptr =NULL;
int msg_tag;
details.id =(char*)malloc(sizeof(char)*40);
ptr = &details;
printf("\nenter msg metadata (tag,year,month,day,id):");
scanf("%d,%d,%d,%d,%s",&msg_tag,&(details.year),&(details.month),&(details.day),details.id);
details.tag = msg_tag;
head = addFirst(ptr,head);
printAll(head);
}
Your code initialises current
to (Node *)NULL, then attempts to dereference the pointer.您的代码将
current
初始化为 (Node *)NULL,然后尝试取消引用该指针。 That's not going to work.那是行不通的。
void printAll(Node* head) {
Node* current = NULL;
((MsgDetails*)current->item)->year = ((MsgDetails*)head->item)->year;
The preliminary code that makes assignments to current
do not seem necessary.对
current
进行分配的初步代码似乎没有必要。 Assign head
to current
and it will work:将
head
分配给current
,它将起作用:
void printAll(Node* head) {
Node* current = head;
while (current) {
printf("%d,%d,%d,%d,%s", ((MsgDetails*)current->item)->tag,((MsgDetails*)current->item)->year,((MsgDetails*)current->item)->month,((MsgDetails*)current->item)->day,((MsgDetails*)current->item)->id);
current = current->next;
}
printf("\n");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.