下面這個程式是簡單使用鏈表的應用程式,但是編譯運行完之后出現,debug assertion failed的錯誤,我在網上看到大家都說有野指標才會出現這種狀況,新手,不知道怎么找,有沒有大神指點一下怎么查找野指標啊?不勝感激
/*編程思想->簡化修改程序*/
/*使用結構鏈表*/
/*通過輸入來確定要輸入的個數,當輸入的電影名是一個空行時,輸入停止*/
#include <stdio.h>
#include <stdlib.h> /*提供malloc()函式原型*/
#include <string.h> /*提供strcpy()函式原型*/
#define TSIZE 45 /*存放片名的陣列大小*/
struct film {
char title[TSIZE];
int rating;
struct film * next; /*指向鏈表的下一個結構*/
};
int main(void)
{
struct film * head = NULL;
struct film * prev = NULL;
struct film * current = NULL;
char input[TSIZE];
/*收集并存盤資訊*/
puts("Enter first movie title: ");
while(gets(input) != NULL && input[0] != '\0')
{
current = (struct film *) malloc (sizeof(struct film));
if(head == NULL)/*第一個結構*/
head = current;
else /*后續結構*/
prev->next = current;
current->next = NULL;
strcpy(current->title, input);
puts("Enter your rating <0-10>");
scanf("%d", ¤t->rating);
while(getchar() != '\n')
continue;
puts("Enter next movie title (empty line to stop): ");
prev = current;
}
/*給出電影串列*/
if(head == NULL)
printf("No data entered.");
else
printf("Here is the movie list: \n");
current = head;
while(current != NULL)
{
printf("Movie: %s Rating:%d.\n", current->title, current->rating);
current = current->next;
}
/*任務已完成,釋放空間*/
current = head;
while(current != NULL)
{
free(current);
current = current->next;
}
printf("Bye!\n");
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/74440.html
標籤:基礎類
上一篇:C++萌新一枚,求指教為什么運行后會忽略最后一組輸入的數
下一篇:CodeGear2007撰寫的VCL程式,在Win7,Win2008等上視窗最大化時,alClient的控制元件不能自適應,切換下又會好了,為什么?怎么解決呢?
