简体   繁体   中英

Segmentation Fault while working with linked list in C

输出在图像中给出。在尝试输入另一个订单时程序停止 I am a newbie to c. The question is to implement a Reastuarent Management Software using Linkedlist. A user should be able to enter their order and view their order. A user who pays additional amount for fast delivery should be appeared on top of the order list.

My code, while executing shows no erros but while running it shows segmentation fault when I try to enter the second entry or try to display the list.


#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct items{
    char *item_name;
    float cost;
};
struct items array[] = {
    {"Pizza",49.9},{"Apples",22.0},{"Oranges",10.5},{"Grapes",3.5},{"Parotta",4.5}
};
struct bill_item{
    int bill_no;
    char* customer_name;
    int order_item_no;
    int quantity;
    float total;
    struct bill_item *next;
};
struct bill_item* head;
int count=0;

void insert_at_start(struct bill_item* node);
void insert_at_end(struct bill_item* node);
void book();
void display_list();

void main(){
    int choice;
    head = (struct bill_item*)malloc(sizeof(struct bill_item));
    head->next = NULL;
    while(choice!=3){
        printf("\n----------------------------------------------");
        printf("\n1.Book\t2.Check Orders\t3.Exit\nEnter option: ");
        scanf("%d",&choice);
        printf("\n------choice %d",choice);
        switch(choice){
            case 1: book();
                    printf("\nItem Purchased!!");
                    break;
            case 2: display_list();
                    break;
            case 3: break;
            default: printf("\nEnter the correct option");
                    break;
        }
    }
}

void insert_at_start(struct bill_item* node){
    if(head->next == NULL){
        head->next = node;
    }        
    else{
        node->next = head;
        head->next = node;
    }
}
void insert_at_end(struct bill_item* node){
    struct bill_item* ptr = head;
    if(head->next==NULL)
        head->next = node;
    else{
        while(ptr->next!=NULL){
            ptr = ptr->next;
        }
        ptr->next = node;
    }
}
void book(){
    char c;
    struct bill_item* node = (struct bill_item*)malloc(sizeof(struct bill_item));
    int i=0,choice;
    printf("\nMenu");
    printf("\n-----------------");
    for (i=0;i<5;i++){
        printf("\n%d %s : %.2f",i+1,array[i].item_name,array[i].cost);
    }
    printf("\nEnter choice: ");
    scanf("%d",&choice);
    printf("\nEnter quantity: ");
    scanf("%d",&node->quantity);
    node->next = NULL;
    count++;
    node->bill_no = count;
    node->total = array[choice-1].cost*node->quantity;
    fflush(stdin);
    printf("\nEnter customer name: ");
    fgets(node->customer_name,30,stdin);
    fflush(stdin);
    printf("\nPurchase amount: %.2f \nNeed fast delivery(Extra 100rs will be charges)(y/n)?",node->total);
    c = getc(stdin);
    fflush(stdin);
    if(c=='Y' || c=='y'){
        node->total +=100;
        printf("\nFast delivery applied\nTotal %.2f",node->total);
        insert_at_start(node);
    }
    else{
        printf("\nTotal: %.2f",node->total);
        insert_at_end(node);
    }
}
void display_list(){
    printf("\nBill No\t\tOrder Item\t\tCname\t\tQ\t\tTotal");
    struct bill_item* ptr = head;
    ptr = ptr->next;
    while(ptr->next!=NULL){
        printf("\n%d\t\t%d\t\t%s\t\t%d\t\t%.2f",ptr->bill_no,ptr->order_item_no,ptr->customer_name,ptr->quantity,ptr->total);
        ptr = ptr->next;
    }
}

I have tried to find the errors. But can't point out any. The errror is somewhere at the functions insert_at_end() and insert_at_start()

In display_list() , if head->next is NULL , you try to read the ptr->next without first checking if it's null:

ptr = ptr->next;
while(ptr->next != NULL) {
// ...
}

This will segfault because ptr is null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM