gpt4 book ai didi

c - 以链表的形式构建字典并收到段错误(核心转储)错误

转载 作者:行者123 更新时间:2023-11-30 16:09:39 25 4
gpt4 key购买 nike

我知道段错误(核心转储)错误是因为我试图访问我无权访问的内存,但我不明白为什么它会对我的程序执行此操作。

我确信问题来自于 printList(node *head) 函数,但我不明白为什么。

在这段代码中,我必须接受用户输入并以链表的形式创建一个字典。完整代码如下:

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

//To count the number of times an element was added to the dictionary
static int countWords=0;

typedef struct node{

char string[255]; //store the data
struct node *next; //pointer to the next structure -> this is a pointer

}node;

//Prints the list
void printList(node *head){

node *p = head;

do{
for(int i=0; i<255; i++)
printf("%c", p->string[i]);
p = p -> next;
}
while(p->next != NULL);

}

//Inserts the word in alphabetical order in the list
node *insert_dictionary_order(char *pWord, node *dictionary){

node *head = dictionary; //head of the first node
node *temp = NULL; //Node that will be placed in the linked list
node *p = NULL; //iterator

//creating individual isolated nodes -> not attached to a list
temp = (node*)malloc(sizeof(node));

//This stores the values that pointer gives us in an array
char word[255];
for(int i=0; i<5; i++)
word[i] = *(pWord + i);

//instantiating the node member
temp->next = NULL;
for(int i=0; i<5; i++)
temp -> string[i] = word[i];

//LinkedList is still empty
if(head == NULL){
printf("\nWe get here4!!\n");
head = temp;
printList(head); //THIS DOESNT WORK
printf("\nWe get here5!!\n");
}
else{
printf("\nWe get here6!!\n");
p = head;
//Comparing temp to words in the dictionary
//To know if we are at the first iteration (changing the head)
do{

printf("New word: %s\nDictionary Word: %s\n", temp -> string, p->string);

if(strcmp(temp->string, p->string) < 0 && countWords ==0){ //Special case for the head
temp -> next = p;
head = temp;
countWords++;
}
//Checking if we are at inserting position
else if(strcmp(temp->string, p->string) > 0 && strcmp(temp->string, (p->next)->string) < 0){
temp->next = p->next;
p->next = temp;
countWords++;
}

p = p->next;
}
while(p->next != NULL);
}
printList(head);
return head;
}

//To check if a certain character is a letter
int isALetter(char c){

if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
c == '\0')
return 1;
else
return 0;
}

int main(){

node *dictionary = NULL;

//Prompting the user to enter strings
printf("Please enter some strings.\n");

//To store the strings that the user enters
char string[10000];
fgets(string, 10000, stdin);

//Putting the string to lower case for easy manipulations
for(int i=0; i<10000; i++)
string[i] = tolower(string[i]);

//To put the word in an array
char word[255];
//looping through the strings
int i=0;
//looping through the word
int j = 0;

//Checking the strings to put the words in the dictionary
do{
//Checking if we need to escape the loop
if(i>0 && ((string[i] == '.' && string[i-1]== ' ' )|| string[i] == 0))
break;

//Checking if a letter
if(isALetter(string[i]) == 0){
i++;
continue;
}

//Checking if end of the word
if(isALetter(string[i+1]) == 0)
{
word[j] = string[i];

printf("\n");
char *pWord;
pWord = word;

printf("\nWe get here1!!\n");
//Adding word to the dictionary
dictionary = insert_dictionary_order(pWord, dictionary);

//***trying stuff
printf("\nWe get here2!!\n");

//***Printing the updated linked list
//printList(dictionary);

//***trying stuff
printf("\nWe get here3!!\n");

//reseting the word array
memset(word, 0, 255);

j = 0;
i++;
continue;
}
word[j] = string[i];
j++;
i++;
}
while(i != 10000);

//Printing the list
printf("\n\nPrinting the list..\n\n");

printList(dictionary);


return 0;
}

这是输出:

Please enter some strings.
zz aa


We get here1!!

We get here4!!
Segmentation fault (core dumped)

我们可以清楚地看到问题来自于具有此输出的 printList(node *head) 方法,因为“We get here!!”数字 2、3、5 和 6 没有出现(它们都是在调用此方法之后出现的)。

最佳答案

正如 kaylum 评论的那样,使用调试器。如果你这样做了,你会看到这个:

Please enter some strings.
aa zz


We get here1!!

We get here4!!

Program received signal SIGSEGV, Segmentation fault.
0x0000555555555334 in printList (head=0x612000000040) at t.c:26
26 while(p->next != NULL);
(gdb) p p
$1 = (node *) 0x0

这就是你的错误的根源(正如 kaylum 也已经评论过的那样)。

关于c - 以链表的形式构建字典并收到段错误(核心转储)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59095387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com