程序在 C 中执行到一半停止
Program stops execution halfway through in C
这可能是一个愚蠢的问题,但基本上这个程序使用指针列表,但在第一次使用我用来打印列表的 showliste
函数后停止执行,我不知道为什么。如果我删除 showliste
函数,那么它会运行其余代码,但我真的不知道为什么,因为我没有修改该函数中的任何内容,它的唯一目的是打印出元素。
如果有人能帮助我,那将非常有用。提前致谢!
这是我的代码:
#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
是列表的头部。
pparcour
是一个指针,我用它来遍历我的列表而不修改它。
pprecedent
基本上就是pparcour
之前的元素,主要用来在有序列表的正确位置添加一本新书(如果新书的价格小于价格位于 pparcour->price
)
这些行
pparcour = pdebut;
/* ... */
pparcour = pparcour->next;
设置对最近 malloc
结构的未初始化 next
成员的访问权限,其中包含不确定的指针值。试图通过这个不确定的指针值
读取 price
成员
while(pparcour != NULL && pnew->price > pparcour->price)
将在循环的后续迭代中调用 Undefined Behaviour。
使用calloc
,或者手动设置新分配节点的next
成员为NULL
。
for(i = 0; i < max && choice == 0; i++)
{
pnew = malloc(sizeof *pnew);
pnew->next = NULL;
/* ... */
这可能是一个愚蠢的问题,但基本上这个程序使用指针列表,但在第一次使用我用来打印列表的 showliste
函数后停止执行,我不知道为什么。如果我删除 showliste
函数,那么它会运行其余代码,但我真的不知道为什么,因为我没有修改该函数中的任何内容,它的唯一目的是打印出元素。
如果有人能帮助我,那将非常有用。提前致谢!
这是我的代码:
#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
是列表的头部。pparcour
是一个指针,我用它来遍历我的列表而不修改它。pprecedent
基本上就是pparcour
之前的元素,主要用来在有序列表的正确位置添加一本新书(如果新书的价格小于价格位于pparcour->price
)
这些行
pparcour = pdebut;
/* ... */
pparcour = pparcour->next;
设置对最近 malloc
结构的未初始化 next
成员的访问权限,其中包含不确定的指针值。试图通过这个不确定的指针值
price
成员
while(pparcour != NULL && pnew->price > pparcour->price)
将在循环的后续迭代中调用 Undefined Behaviour。
使用calloc
,或者手动设置新分配节点的next
成员为NULL
。
for(i = 0; i < max && choice == 0; i++)
{
pnew = malloc(sizeof *pnew);
pnew->next = NULL;
/* ... */