【问题标题】:How to Copy Characters in a Linked List to an Array in C?如何将链表中的字符复制到 C 中的数组中?
【发布时间】:2020-02-29 02:53:41
【问题描述】:

我在将链表中的字符数组的内容复制到常规字符数组时遇到问题。我有一个分段错误的问题,我不知道为什么。

我创建的程序在链表中的字符数组只有一个字符时可以工作,但是大于1时就不行。主要问题出现在第62行("array[index] = p -> 字[计数]")。我曾尝试使用 strcpy 将它的每个索引复制到字符数组中,但这也产生了一个错误,内容为:“传递‘strcpy’的参数 2 使指针从整数而不进行强制转换”。 但是,当我使用赋值语句时,我只会遇到分段错误。我不知道为什么,因为我觉得我已经创建了足够的内存,应该能够保存数组的链表内容。

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

typedef struct node
{
    char word[100];
    struct node *next;
} ListNode;

int main ()
{
    ListNode * head = NULL;
    ListNode * tail = NULL;

    for (int count = 0; count < 5; count++)
    {
        ListNode * temp = malloc (sizeof (*temp));

        strcpy(temp -> word, "Hi");
        temp -> next = NULL;

        if (tail == NULL)
        {
            head = temp;
            tail = temp;
        }
        else
        {
            tail->next = temp;
            tail = temp;
        }
    }

    char array[999]; // array that will hold the characters in the linked list

    ListNode * p = head; //position of the current node
    int count;
    int index = 0;


    // while p is still a node in the list
    while(p != NULL)
    {
        if((int) strlen(p -> word) > 1) // checks if the string is longer than one character
        {
          count = 0; // initializes count as 0

          while(count < (int) strlen(p -> word)) // counts how many characters are in the string
          {
            array[index] = p -> word[count]; // assings the words into charater array
            count++; // increments the count
            index++; // changes the index
          }
        }
        else
        {
          array[index] = p -> word[0]; // copies p-word to array
          index++; // changes the index in the array
          p = p -> next;
       }
    }

    return 0;
}

如前所述,只要链表中的字符数组只有1,程序就可以工作,但是当数字大于1时会产生分段错误。请让我知道我需要在这个程序中更正什么。谢谢。

【问题讨论】:

  • p = p -&gt; next; 是有条件的。 -->> Intead,你可以使用for循环来避免这种逻辑错误。
  • ListNode * temp = malloc (sizeof (*temp));。这个声明是什么意思?您正在创建大小为 (*temp) 的内存,但 temp 是什么?
  • 创建空间来保存 ListNode @problematicDude 的内容
  • 编译器如何知道 *temp 的大小?编译器不知道什么是 temp。
  • 这不是问题。并且 temp 已经被声明为 ListNode。因此,它确实知道 temp 是什么。 ListNode * temp 创建一个指针。使用 malloc(sizeof(*temp)) 时,我创建空间来保存字符数组和下一个节点的指针。

标签: c arrays string linked-list


【解决方案1】:
  • 简化你的循环; for-loops 允许您将循环机制保持在一条线上
  • 避免特殊情况;单字符字符串没有什么特别之处

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

typedef struct node
{
  char word[100];
  struct node *next;
} ListNode;

int main ()
{
  ListNode * head = NULL;
  ListNode * tail = NULL;
  ListNode * p ;
  int count;
  int index ;
  char array[999]; // array that will hold the characters in the linked list

  for (count = 0; count < 5; count++)
  {
    ListNode * temp = malloc (sizeof *temp);

    strcpy(temp->word, "Hi");
    temp->next = NULL;

    if (!tail) { head = temp; tail = temp; }
    else { tail->next = temp; tail = temp; }
  }

  count=0;
  for(p=head;p; p=p->next) { // walk the linked list

      for(index=0;  p->word[index]; index++) { // walk the string
        array[count++] = p->word[index];
      }

    }
  array[count++] = 0; // terminate

  printf("%s\n", array);
  return 0;
}

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2020-09-10
    • 2015-03-23
    • 2015-06-06
    相关资源
    最近更新 更多