gpt4 book ai didi

c - 为结构指针执行 malloc 时出现段错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:03:41 35 4
gpt4 key购买 nike

我正在尝试编写基本的链表代码。我有两个函数可以将数据添加到列表的开头和结尾。在开头添加数据的功能每次都能正常工作。

我总是在 insert_end 函数中遇到段错误。在对它执行 malloc 之后尝试访问指向结构的 temp1 指针时出现错误。但这与我在 insert_first 函数中所做的完全相同,但每次都有效。我试着用谷歌搜索它并在论坛上试过,没有答案。请帮忙。

这个链接是我的确切问题..但我不完全理解解决方案

Segmentation fault in C with malloc

特别是在这个 block 上我遇到了错误

struct node *temp1,*trav;
temp1 = (struct node *)malloc(sizeof(struct node));
trav = (struct node *)malloc(sizeof(struct node));
if (temp1 != NULL) { //******************Segmentation fault at this line**********
temp1->data = input;
}






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

//*******Structure to hold linked list*********//
struct node {
int data;
struct node *next;
}*head,*temp;


//***********Function to Display everything**********//

void display(struct node *head) {
struct node *trav;
trav= (struct node *)malloc(sizeof(struct node));
printf("Entering into Display\n");
if (head == NULL) {
printf("Oh My God, the list is empty\n");
}
else {
trav = head;
while (trav != NULL) {
printf("Value stored in [%p] is [%d]\n",trav,trav->data);
trav = trav->next;
}
}
}


//***********Function to Insert at beginning*********//

struct node *insert_first(struct node *head,int input) {
temp = (struct node *)malloc(sizeof(struct node));
temp->data = input;
printf("\nEntering insert first");
if (head == NULL) {
head = temp;
head->next = NULL;
}
else {
temp->next = head;
head = temp;
}
return head;
}


//**************Function to Insert at End******************//
struct node *insert_last(struct node *head,int input) {
struct node *temp1,*trav;
temp1 = (struct node *)malloc(sizeof(struct node));
trav = (struct node *)malloc(sizeof(struct node));
if (temp1 != NULL) {
temp1->data = input;
}
else {
printf("empty");
}
printf("\nEntering insert last");
if (head == NULL) {
head = temp1;
head->next = NULL;
}
else {
trav = head;
while (trav != NULL) {
trav = trav->next;
}
trav->next = temp1;
}
return head;
}




//*************Main Fucntion***********//

int main() {
int choice,value;
head = NULL;
while(1) {
printf("\n******Please Enter your choice****\n1. To insert at beginning\n2. To insert at End\n3. To Insert middle\n4. To delete\n5. To display\n0. To Exit\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Please Enter the value to be added\n");
scanf("%d",&value);
head = insert_first(head,value);
break;
case 2:
printf("Please Enter the value to be added\n");
scanf("%d",&value);
head = insert_last(head,value);
break;

case 5:
display(head);
break;
case 0:
return 0;
default:
printf("Thats a wrong choice\n");
break;
}
}
}

最佳答案

这是有问题的 block 。

else {
trav = head;
while (trav != NULL) {
trav = trav->next;
}
trav->next = temp1;

当您从 while 循环中出来时,travNULL。这使得线

    trav->next = temp1;

因分段违规而失败。

将该 block 更改为:

else {
trav = head;
while (trav->next != NULL) {
trav = trav->next;
}
trav->next = temp1;

关于c - 为结构指针执行 malloc 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23307278/

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