简体   繁体   English

程序在 C 中中途停止执行

[英]Program stops execution halfway through in C

This might be a dumb question but basically this program which uses pointer lists but stops execution after the first use of my showliste function which I use to I print out the list and I have no idea why.这可能是一个愚蠢的问题,但基本上这个程序使用指针列表,但在第一次使用我用来打印列表的showliste函数后停止执行,我不知道为什么。 If I remove the showliste function then it runs the rest of the code just fine however I really have no idea why since I don't modify anything in that function and its only purpose is to print out the elements.如果我删除了showliste函数,那么它运行其余代码就好了,但是我真的不知道为什么,因为我没有修改该函数中的任何内容,它的唯一目的是打印出元素。

If somebody could help me out that would be very useful.如果有人可以帮助我,那将非常有用。 Thank you in advance!先感谢您!

Here is my code:这是我的代码:

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

#define max 10

struct book{
    char name[50];
    float price;
    struct book *next;
};
typedef struct book BOOK;

BOOK * createlist(BOOK *);
void showliste(BOOK *);
BOOK * deleteElem(BOOK *);

int main()
{
    BOOK *pstart = NULL;
    pstart = createlist(pstart);
    printf("\nHere is the ordered list: \n");
    showliste(pstart); //stops excution after this for some reason
    pstart = deleteElem(pstart);
    printf("\nHere is the list with the element deleted: \n");
    showliste(pstart);
    return 0;
}

BOOK * createlist(BOOK *pdebut)
{
    int i, choice = 0;
    BOOK *pparcour = NULL, *pprecedent = NULL, *pnew = NULL;
    for(i = 0; i < max && choice == 0; i++)
    {
        pnew = (BOOK *)malloc(sizeof(BOOK));
        printf("Enter name: ");
        fflush(stdin);
        gets(pnew->name);
        printf("Enter Price: ");
        scanf("%f", &pnew->price);
        if(pdebut == NULL)
        {
            pdebut = pnew;
        }
        else{
            pparcour = pdebut;
            pprecedent = NULL;
            while(pparcour != NULL && pnew->price > pparcour->price)
            {
                pprecedent = pparcour;
                pparcour = pparcour->next;
            }
            if(pprecedent == NULL)
            {
                pnew->next = pparcour;
                pdebut = pnew;
            }
            else{
                pprecedent->next = pnew;
                pnew->next = pparcour;
            }
        }
        printf("Do you want to continue? \n");
        printf("0 - Yes                1 - NO\n");
        printf("Choice: ");
        scanf("%d", &choice);
    }
    return pdebut;
}

void showliste(BOOK *pdebut)
{
    while(pdebut != NULL)
    {
        printf("Name: %s\n", pdebut->name);
        printf("Price: %.3f\n\n", pdebut->price);
        pdebut = pdebut->next;
    }
}

BOOK * deleteElem(BOOK *pdebut)
{
    char cible[50];
    BOOK *pprecedent = NULL, *pparcour = NULL;
    printf("Enter the name of the book you want to delete: ");
    fflush(stdin);
    gets(cible);
    pparcour = pdebut;
    pprecedent = NULL;
    while(pparcour != NULL && strcmpi(cible, pparcour->name))
    {
        pprecedent = pparcour;
        pparcour = pparcour->next;
    }
    if(pparcour == NULL)
    {
        printf("\nEntered name is not in the list!!!!\n");
    }
    else{
        if(pprecedent == NULL)
        {
            pdebut = pdebut->next;
            free(pparcour);
        }
        else{
            pprecedent->next = pparcour->next;
            free(pparcour);
        }
    }
    return pdebut;
}
  • pdebut is the head of the list. pdebut是列表的首位。
  • pparcour is a pointer which I use to go through my list without modifying it. pparcour是一个指针,我用它来浏览我的列表而不修改它。
  • pprecedent is basically the element just before pparcour , mainly used to add a new book in the correct position in a ordered list (if the price of the new book is smaller than the price located in pparcour->price ) pprecedent基本上是pparcour之前的元素,主要用于在有序列表中的正确位置添加新书(如果新书的价格小于位于pparcour->price中的价格)

These lines这些线

pparcour = pdebut;
/* ... */
pparcour = pparcour->next;

setup access to the uninitialized next member of the recently malloc 'd structure, which contains an indeterminate pointer value.设置对最近malloc结构的未初始化next成员的访问,该结构包含一个不确定的指针值。 Attempting to to read a price member via this indeterminate pointer value试图通过这个不确定的指针值读取price成员

while(pparcour != NULL && pnew->price > pparcour->price)

will invoke Undefined Behaviour on subsequent iterations of the loop.将在循环的后续迭代中调用未定义行为

Use calloc , or manually set the newly allocated node's next member to NULL .使用calloc ,或者手动将新分配的节点的next成员设置为NULL

for(i = 0; i < max && choice == 0; i++)
{
    pnew = malloc(sizeof *pnew);
    pnew->next = NULL;
    /* ... */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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