【问题标题】:Why does the singly linked list print "NULL"?为什么单链表打印“NULL”?
【发布时间】:2021-03-07 11:04:15
【问题描述】:

我编写了一个代码,它打印单链表数组中的单词。但是当我运行代码时,它会打印(null) 而不是JAN。我不知道为什么它一直打印null。谁能帮帮我?

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

struct LLNode
{
    char *CharArr[10];
    struct LLNode *next;
};

struct LLNode * createNode (char val[])
{
    struct LLNode *temp;
    temp =(struct LLNode *)malloc(sizeof(struct LLNode));
    temp-> CharArr[10] = val;
    temp-> next = NULL;
    return (temp) ;
};

int main ()
{
    struct LLNode *head = NULL;
    struct LLNode *curr = NULL;
    //char a[10]="JAN";

    head = curr = createNode ("JAN") ;
    printf ("curr->CharArr[10] = %s\n", curr->CharArr[10]) ;
}

【问题讨论】:

  • 用你自己的话来说:你认为curr-&gt;CharArr[10] 是什么意思?
  • 另外:当你在结构体中声明char *CharArr[10]时,你期望结构体包含什么?

标签: c singly-linked-list


【解决方案1】:

超出范围的索引。你的程序是未定义的,但它被简化为非常简单的东西。

    temp-> CharArr[10] = val;
    temp-> next = NULL;

CharArr 只有十个元素,而你写给了不存在的第十个元素,所以当你写给temp-&gt;next 时,你覆盖了val

也许您认为数组的索引是从 1 到 n,但它们的索引是从 0 到 n。

要查看这项工作,请将char *CharArr[10]; 更改为char *CharArr[11];。但是您可能想完全重新考虑该类型。看起来您的意思是 char CharArr[10]; 并通过调用 strcpy (#include &lt;string.h&gt;) 来填充它。

【讨论】:

  • 哦,所以如果我想让val被写入,那么我应该在初始化char *CharArr[n]之后考虑CharArr[(n-1)](n可以是任何数字)。对吗?
  • @lyhsuan01:正确。
【解决方案2】:

我不确定你想用CharArr 实现什么:要么你的节点携带数据,而你摆脱了指向 char 数组的指针,或者你希望它成为指向另一个区域的指针字符串所在的位置,去掉数组部分。

第一个版本,节点包含字符:

#include <stdio.h>
#include <stdlib.h>

struct LLNode
{
    char *CharArr[10];
    struct LLNode *next;
};

struct LLNode * createNode (char val[])
{
    struct LLNode *temp;
    temp =(struct LLNode *)malloc(sizeof(struct LLNode));
    temp-> CharArr[10] = val;
    temp-> next = NULL;
    return (temp) ;
};

int main ()
{
    struct LLNode *head = NULL;
    struct LLNode *curr = NULL;

    head = curr = createNode ("JAN") ;
    printf ("curr->CharArr[10] = %s\n", curr->CharArr[10]) ;
}

第二个版本,指向Node外的一个区域:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct LLNode
{
    char *CharArr;
    struct LLNode *next;
};

struct LLNode * createNode (char val[])
{
    struct LLNode *temp;
    temp =(struct LLNode *)malloc(sizeof(struct LLNode));
    temp->CharArr = strdup(val);
    temp-> next = NULL;
    return (temp) ;
};

int main ()
{
    struct LLNode *head = NULL;
    struct LLNode *curr = NULL;
    //char a[10]="JAN";

    head = curr = createNode ("JAN") ;
    printf ("curr->CharArr = %s\n", curr->CharArr) ;
}

请注意,在这两种情况下,都不会对节点进行清理。在第二个版本中,还需要free()CharArr

查看 valgind 输出:

$ valgrind ./so 
==19670== Memcheck, a memory error detector
==19670== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==19670== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==19670== Command: ./so
==19670== 
curr->CharArr = JAN
==19670== 
==19670== HEAP SUMMARY:
==19670==     in use at exit: 20 bytes in 2 blocks
==19670==   total heap usage: 3 allocs, 1 frees, 1,044 bytes allocated
==19670== 
==19670== LEAK SUMMARY:
==19670==    definitely lost: 16 bytes in 1 blocks
==19670==    indirectly lost: 4 bytes in 1 blocks
==19670==      possibly lost: 0 bytes in 0 blocks
==19670==    still reachable: 0 bytes in 0 blocks
==19670==         suppressed: 0 bytes in 0 blocks
==19670== Rerun with --leak-check=full to see details of leaked memory
==19670== 
==19670== For lists of detected and suppressed errors, rerun with: -s
==19670== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

【讨论】:

  • 嗯...实际上我想定义一个struct,其中包括一个大小为10的字符数组(用于存储字符串)和一个指向下一个节点的指针。但是如果我去掉char *CharArr[10]的指针,它也会显示(null)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 2023-01-19
  • 2019-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多