【问题标题】:Segmentation fault when trying to compare strings - C Programming尝试比较字符串时出现分段错误 - C 编程
【发布时间】:2022-11-18 04:30:43
【问题描述】:

我正在使用链表进行排序功能。基本上我会输入一个字符串,然后按回车键,它就会在链接列表中注册。第一个进入的将成为链表的头部。

当我输入后续字符串时,我想将它放在较大的字符串之前。如果它大于第一个现有字符串,我将向下移动下一个链表并与第二个现有链表进行比较。如果它较小,我会把它放在前面。

所以如果这样输入

cat
dog
bird

它会这样输出

cat
bird
dog

但是,只能比较前两者。如果我比较第三个,它会抛出一个分段错误并结束程序。

似乎无法理解为什么我的指针之一存在分段错误->字符(ptr->字符)。我可以完美地打印出这个字符,但我无法比较它,因为它给出了一个分段错误。


#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX

char userInput[33];
char stopStr[] = "***";
int isInserted = 0;

struct node {
  char characterInput[33];
  // int data;
  struct node *link;
};

void printData(struct node *head) {
  printf("All the entered words in order:");
  if (head == NULL) {
    printf("\nLink List is empty");
  }
  struct node *ptr = NULL;
  ptr = head;
  while (ptr != NULL) {
    printf("\n%s", ptr->characterInput);
    ptr = ptr->link;
  }
}

int main(void) {
  // creating a head
  struct node *head = malloc(sizeof(struct node));

  scanf("%s", &userInput);
  if (strcmp(userInput, stopStr) != 0) {

    for (int i = 0; i < strlen(userInput); i++) {
      userInput[i] = tolower(userInput[i]);
    }
    strcpy(head->characterInput, userInput);
    head->link = NULL;
  } else {
    return 0;
  }

  while (1) {
    struct node *current = malloc(sizeof(struct node));
    struct node *ptr = malloc(sizeof(struct node));
    struct node *ptr2 = NULL;
    ptr = head;
    isInserted = 0;

    scanf("%s", &userInput);
    if (strcmp(userInput, stopStr) == 0) {
      break;
    }

    // convert to lowercase
    for (int i = 0; i < strlen(userInput); i++) {
      userInput[i] = tolower(userInput[i]);
    }
    // insert userinput to node
    strcpy(current->characterInput, userInput);

    //trying to compare between strings in each linked list. If linked list current string is smaller than the string in head, link current to head and make head = current
    if (strcmp(head->characterInput, userInput) > 0) {
      current->link = head;
      head = current;
    } else if (ptr->link == NULL) {
      ptr->link = current;
//else put it at the back of the head if there is no linked list at the back. (if head->link is null)
    }
    else{
      while(isInserted == 0){
        
        ptr2 = ptr;
        
        ptr = ptr->link;
        printf("\n%s",ptr->characterInput); 
//here's the issue, I can print out ptr->character but i cant compare them below, in the if statement against my current user input.
        if(strcmp(ptr->characterInput,userInput)>0){
          
          ptr2->link = current;
          current->link = ptr;
          isInserted = 1;
        }
        
      }
    }
  }

  printData(head);
}

【问题讨论】:

  • main 函数中,您有 struct node *ptr = malloc(sizeof(struct node)); 后跟 ptr = head;。这将使您失去由malloc 分配的内存。它是一个内存泄漏.我建议您退后一步,更多地研究指针及其工作原理。
  • 至于崩溃,请学习如何使用 debugger 来捕获崩溃,定位崩溃发生在代码中的时间和位置,并检查变量及其值以及崩溃的位置和时间。还学习如何使用调试器逐行调试代码,同时监视变量及其值。

标签: c linked-list


【解决方案1】:

什么一些程序员花花公子说。 + 您没有注意新元素进入链表尾部的替代方案。你应该让关联当前的分配后立即为 NULL。而在同时(isInserted == 0)循环处理这种情况ptr->链接一片空白。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-21
    • 2016-06-16
    • 1970-01-01
    • 2015-05-20
    • 2011-09-03
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多