写了一个记录 电影名称 种类 等等, 并且把电影名称按照字母由小到大来排列的程序
这个是电影资料的结构
typedef struct info
{
char Title[50];
char Genre;
int PlayCount;
struct info *link;
} Info, *infoptr;
这个是在main中执行函数的语句
infoptr interptr = NULL;
interptr = getNewMovie( input2, GetGenre, interptr );
下面就是有问题的函数
infoptr getNewMovie( const char movie[], char genre, infoptr ptr)
{
infoptr NewMovie = malloc(sizeof(Info));
infoptr before = ptr;
infoptr after = ptr;
int count=0;
strcpy(NewMovie->Title, movie);
NewMovie->Genre = genre;
if(ptr == NULL)
{
NewMovie->link = NULL;
return NewMovie;
}
else
{
if(strcmp(NewMovie->Title, before->Title) > 0)
{
for(; (strcmp(NewMovie->Title, after->Title) > 0) && (after != NULL); count++)
{
after = after->link;
}
for(count – 1; count > 0; count–)
{
before = before->link;
}
before->link = NewMovie;
NewMovie->link = after;
return ptr;
}
else
{
NewMovie->link = ptr;
return NewMovie;
}
}
都是在运行上面这段的时候会出现bus error
>> 本文固定链接: http://www.vcgood.com/archives/3312
如果需要 这是更详细一点的main
void safegets (char s[], int arraySize); // gets without buffer overflow
void movieTitleDuplicate (char movieTitle[]);
void movieTitleNotFound (char movieTitle[]);
void movieBeingPlayed (char movieTitle[]);
void printEyeTwoNzEmpty (void);
void printEyeTwoNzTitle (void);
void printNoMoviesPlayed (void);
//**********************************************************************
// Program-wide Constants
//
const int MAX_LENGTH = 1023;
const char NULL_CHAR = ’\0′;
const char NEWLINE = ’\n’;
//**********************************************************************
// Main Program
//
int main (void)
{
const char bannerString[]
= ”EyeTwoNz Movie Collection Program.\n\n”;
const char commandList[]
= ”Commands are I (insert), D (delete), P (play),”
” L (list all entries),\n H (list entry most played),”
” X (list entries for first years), Q (quit).\n”;
// announce start of program
printf(“%s”,bannerString);
printf(“%s”,commandList);
char response;
char input[MAX_LENGTH+1];
char input2[MAX_LENGTH+1];
infoptr interptr = NULL;
bool First = true;
do
{
printf(“\nCommand?: ”);
safegets(input,MAX_LENGTH+1);
response = toupper(input[0]);
if (response == ’I')
{
char GetGenre;
printf(“ title: ”);
safegets(input2,MAX_LENGTH+1);
printf(“ genre: ”);
scanf(“%c”,&GetGenre);
infoptr searchlink;
bool Duplicate = false;
if(!First)
{
for(searchlink = interptr; (searchlink != NULL) && (First == false) && (Duplicate == false); searchlink = searchlink->link)
{
if(strcmp(input2, searchlink->Title) == 0)
{
movieTitleDuplicate (input2);
Duplicate = true;
}
}
}
if(!Duplicate)
{
interptr = getNewMovie( input2, GetGenre, interptr );
}
First = false;
}
大概看了下,这句好像有问题:
{
before = before->link;
}
count-1是干什么的?想让count减去1?