C 避免链表中的垃圾值

C 避免链表中的垃圾值,c,struct,linked-list,printf,nodes,C,Struct,Linked List,Printf,Nodes,假设一个单词在词典中有多种含义。实际上,在我的字典中,一个节点有5种含义。因此,当我查找单词时,程序将打印2个含义以及其他3个100个字符长的垃圾值 我怎样才能避免它们被打印出来 这是我的密码: struct node{ char word[20]; char meaning[5][100]; struct node *next; }; void lookup(struct node *head, char *word) { int found = 0, i; w

假设一个单词在词典中有多种含义。实际上,在我的字典中,一个节点有5种含义。因此,当我查找单词时,程序将打印2个含义以及其他3个100个字符长的垃圾值

我怎样才能避免它们被打印出来

这是我的密码:

struct node{
    char word[20];
    char meaning[5][100];
    struct node *next;
};
void lookup(struct node *head, char *word)
{
  int found = 0, i;
  while(head != NULL)
  {
    if(strcmp(head->word, word) == 0)
    {
        found = 1;
        printf("\n\t%s", word);
        for(i = 0; i < 5; i++) printf("\n\t%s", head->meaning[i]);
        printf("\n");
        break;
    }
    head = head->next;
  }
  if(found == 0) printf("\nWord not found in the dictionary!!");
}
struct节点{
字符字[20];
char表示[5][100];
结构节点*下一步;
};
无效查找(结构节点*头,字符*字)
{
int=0,i;
while(head!=NULL)
{
if(strcmp(head->word,word)==0)
{
发现=1;
printf(“\n\t%s”,word);
对于(i=0;i<5;i++)printf(“\n\t%s”,head->意思是[i]);
printf(“\n”);
打破
}
头部=头部->下一步;
}
如果(find==0)printf(“\n字典中找不到单词!!”);
}

在malloc之后使用calloc或do memset分配节点元素,以便默认情况下节点的所有内存(或成员)都使用0填充

struct node *node_element = (struct node *) calloc(1, sizeof(struct node));
然后通过检查长度打印含义

for(i = 0; i < 5; i++) {
  if(strlen(head->meaning[i]) > 0)
    printf("\n\t%s", head->meaning[i]);
}
(i=0;i<5;i++)的
{
如果(strlen(head->means[i])>0)
printf(“\n\t%s”,head->意思是[i]);
}

无法使用NUL终止符(猜测)初始化结构字符串。非空-使用零-->“0”NULL具有不同的含义。参见“man calloc”