通过结构一访问数据到结构二?
Accessing data through struct one to struct two?
首先我开始学习计算机科学,C是主要语言。我在工作的第一天了解到,您必须做很多“脚部工作”才能用 C 完成工作。现在我们正在使用链接列表并进行了练习,我想完成。所以,如果你们中的一些人可以看看我的问题并提供一些关于如何完成该问题的提示,我将非常高兴。
如果你还在读这篇文章,这是我的问题:
我们得到了 2 个结构:
typedef struct MsgDetails {
char *id;
msgtag tag;
int year;
int month;
int day;
} MsgDetails;
typedef struct Node {
void *item;
struct Node *next;
} Node;
这是函数:
struct Node* addMsgDetails(Node* head ,MsgDetails *MsgDetaillos) {
int tag = 0, year = 0, month = 0, day = 0;
char id;
Node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof(Node));
current->next->next = NULL;
current->next->item = malloc(sizeof(MsgDetails));
printf("\nenter msg metadata (tag,year,month,day,id):");
scanf(" %d, %d, %d, %d, %40s", &tag, &year, &month, &day, &id);
MsgDetaillos->tag = tag;
MsgDetaillos->year = year;
MsgDetaillos->month = month;
MsgDetaillos->day = day;
MsgDetaillos->id = &id;
return current;
}
所以问题是我知道当我想添加一个新结构时我需要为每个结构分配内存。我循环遍历我的节点结构,直到我到达下一个指针中为 NULL 的节点。现在我在正确的节点中,我还在 void *item 指针中添加了分配内存的地址,但现在我不知道如何将 Msg Details 结构的信息添加到 item 变量中。希望这不会太混乱?
我试图找到问题,但实际上也不知道如何寻找。我尝试过“通过另一个结构从一个结构调用数据”和类似的事情,但无法得到答案。如果有任何帮助,我将不胜感激!
- 您会将充满数据的缓冲区而不是未初始化的缓冲区分配给结构成员。
char
不能容纳正长度的字符串。您将必须为字符串分配缓冲区。
而不是这个
current->next->item = malloc(sizeof(MsgDetails));
printf("\nenter msg metadata (tag,year,month,day,id):");
scanf(" %d, %d, %d, %d, %40s", &tag, &year, &month, &day, &id);
MsgDetaillos->tag = tag;
MsgDetaillos->year = year;
MsgDetaillos->month = month;
MsgDetaillos->day = day;
MsgDetaillos->id = &id;
你应该使用这个:
// allocate buffer for data
MsgDetails* data = malloc(sizeof(MsgDetails));
// allocate buffer for string
char* id_str = calloc(41, sizeof(char));
// check results of malloc() and calloc() here fore better safety
printf("\nenter msg metadata (tag,year,month,day,id):");
scanf(" %d, %d, %d, %d, %40s", &tag, &year, &month, &day, id_str);
// fill the buffer with data read
data->tag = tag;
data->year = year;
data->month = month;
data->day = day;
data->id = id_str;
// assign the buffer to the member of the structure
current->next->item = data;
首先我开始学习计算机科学,C是主要语言。我在工作的第一天了解到,您必须做很多“脚部工作”才能用 C 完成工作。现在我们正在使用链接列表并进行了练习,我想完成。所以,如果你们中的一些人可以看看我的问题并提供一些关于如何完成该问题的提示,我将非常高兴。
如果你还在读这篇文章,这是我的问题:
我们得到了 2 个结构:
typedef struct MsgDetails {
char *id;
msgtag tag;
int year;
int month;
int day;
} MsgDetails;
typedef struct Node {
void *item;
struct Node *next;
} Node;
这是函数:
struct Node* addMsgDetails(Node* head ,MsgDetails *MsgDetaillos) {
int tag = 0, year = 0, month = 0, day = 0;
char id;
Node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof(Node));
current->next->next = NULL;
current->next->item = malloc(sizeof(MsgDetails));
printf("\nenter msg metadata (tag,year,month,day,id):");
scanf(" %d, %d, %d, %d, %40s", &tag, &year, &month, &day, &id);
MsgDetaillos->tag = tag;
MsgDetaillos->year = year;
MsgDetaillos->month = month;
MsgDetaillos->day = day;
MsgDetaillos->id = &id;
return current;
}
所以问题是我知道当我想添加一个新结构时我需要为每个结构分配内存。我循环遍历我的节点结构,直到我到达下一个指针中为 NULL 的节点。现在我在正确的节点中,我还在 void *item 指针中添加了分配内存的地址,但现在我不知道如何将 Msg Details 结构的信息添加到 item 变量中。希望这不会太混乱?
我试图找到问题,但实际上也不知道如何寻找。我尝试过“通过另一个结构从一个结构调用数据”和类似的事情,但无法得到答案。如果有任何帮助,我将不胜感激!
- 您会将充满数据的缓冲区而不是未初始化的缓冲区分配给结构成员。
char
不能容纳正长度的字符串。您将必须为字符串分配缓冲区。
而不是这个
current->next->item = malloc(sizeof(MsgDetails));
printf("\nenter msg metadata (tag,year,month,day,id):");
scanf(" %d, %d, %d, %d, %40s", &tag, &year, &month, &day, &id);
MsgDetaillos->tag = tag;
MsgDetaillos->year = year;
MsgDetaillos->month = month;
MsgDetaillos->day = day;
MsgDetaillos->id = &id;
你应该使用这个:
// allocate buffer for data
MsgDetails* data = malloc(sizeof(MsgDetails));
// allocate buffer for string
char* id_str = calloc(41, sizeof(char));
// check results of malloc() and calloc() here fore better safety
printf("\nenter msg metadata (tag,year,month,day,id):");
scanf(" %d, %d, %d, %d, %40s", &tag, &year, &month, &day, id_str);
// fill the buffer with data read
data->tag = tag;
data->year = year;
data->month = month;
data->day = day;
data->id = id_str;
// assign the buffer to the member of the structure
current->next->item = data;