【问题标题】:Struct Char not Assigning properly结构字符未正确分配
【发布时间】:2017-02-11 00:12:03
【问题描述】:

我正在尝试创建一个链表类型的数据结构,目前它只有一个 char 作为数据,但我无法正确分配它。当我运行以下代码时:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

FILE* outfile;
FILE* infile;

int linkedlist_size;
struct linked_list
{
    char* data;
    struct linked_list* next;
    struct linked_list* previous;
};

int main()
{
    outfile = fdopen(STDOUT_FILENO,"w");
    infile = fdopen(STDIN_FILENO,"r");

    struct linked_list line_buffer;
    struct linked_list* current = &line_buffer;
    int linkedlist_size = 0;

    int input_char;
    char input_cast;
    for (input_char = fgetc(infile); (input_char != EOF && input_char != '\n') ;input_char = fgetc(infile))
    {
        input_cast = input_char;
        current->data = malloc(sizeof(char));
        (current->data)[0] = input_cast;
        linkedlist_size++;
        current->next = malloc(sizeof(struct linked_list));
        current = current->next;
        printf("\nMy address is: %p",current);
        printf("\nMy number is: %d",input_char);
        printf("\nMy char cast is: %c",input_cast);
        printf("\nMy char is: %s",current->data);
    }

    return 0;
}

使用gcc ll_test.c 编译,使用./a.out 运行,并使用something 作为键盘输入,我得到以下输出:

My address is: 0x10558a0
My number is: 115
My char cast is: s
My char is: (null)
My address is: 0x1055cf0
My number is: 111
My char cast is: o
My char is: (null)
My address is: 0x1055d30
My number is: 109
My char cast is: m
My char is: (null)
My address is: 0x1055d70
My number is: 101
My char cast is: e
My char is: (null)
My address is: 0x1055db0
My number is: 116
My char cast is: t
My char is: (null)
My address is: 0x1055df0
My number is: 104
My char cast is: h
My char is: (null)
My address is: 0x1055e30
My number is: 105
My char cast is: i
My char is: (null)
My address is: 0x1055e70
My number is: 110
My char cast is: n
My char is: (null)
My address is: 0x1055eb0
My number is: 103
My char cast is: g
My char is: (null)

这意味着字母正确地进入STDIN,被正确解释(输入\n 后循环停止)并且转换正确完成,但分配不起作用。对于它的价值,我还尝试将linked_list.data 设为常规char 并直接分配(通过current-&gt;data = input_cast)并收到类似的结果(空白输出,而不是(null),暗示\0 被“打印” )。我认为这是关于我不熟悉的结构的一些挑剔点,但我无法终生弄清楚它是什么。随意抓取/编译/测试代码。

另外,我知道存在内存泄漏......这是从更大的代码体中修改的 sn-p,所以许多功能都不是学术上的完美。我只是想展示我得到的行为。

谢谢大家!

编辑:如下所述,错误是我在切换到下一个空节点后尝试打印当前节点的字符。我的愚蠢的逻辑错误。

【问题讨论】:

  • 在打印所有数据时,current 指向一个新分配的、未初始化的结构...而不是您填写的那个。分配一个新的结构通常更有意义节点在填充和添加它之前,而不是在每个添加一个节点之后分配下一个节点。

标签: c linux unix struct pass-by-reference


【解决方案1】:
printf("\nMy char is: %s",current->data); 

应该是

printf("\nMy char is: %c", *(current->data)); 

printf("\nMy char is: %c", current->data[0]); 

也就是说,格式说明符应该是单个char 而不是字符串,并且需要取消引用数据指针才能获取字符。如果仍然不清楚,C 中的字符串是 NUL 终止 字符序列。你只有一个字符而不是字符串。

【讨论】:

  • 两个建议的更改都会导致段错误 =(
  • 你把%s改成%c了吗?
  • 是的,两者都有。不过,我不确定为什么这会导致段错误。我确实取消了data 成员的引用,但由于malloc,它至少应该指向一个有效的地址......
  • 您确定是导致 seg 错误的线路吗?您是否在调试器中运行程序来验证这一点?
  • 有一条评论已经指出了你的另一个问题:current = current-&gt;next; 你的printf 语句需要在该行之前。
【解决方案2】:

您需要分配 2 个字节,如下所示:current-&gt;data = malloc(2);第一个字节将存储您的字符,第二个字节将存储字符串终止符'\0',之后您可以将其打印为字符串。您忘记使用之前的字段,您可以:

 current->next = malloc(sizeof(struct linked_list));
 current->next->previous=current;
 current = current->next;

您从新分配的节点打印您的字符串,它没有及时初始化您打印它。将您的行 current = current-&gt;next; 移到 printf 语句上方。

【讨论】:

  • 我分配了额外的字节并将[1]初始化为\0,但行为没有改变。我还在循环中的打印语句之前添加了sleep(1);。仍然出现 null =/
  • @botch 你从新分配的空节点打印,阅读更新的答案
猜你喜欢
  • 2014-08-26
  • 2011-07-13
  • 1970-01-01
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多