0

我创建了以下程序http://pastie.org/5081517这是一个链接列表(议程),按字母顺序排列联系人并让用户搜索特定联系人。

然后我尝试用函数制作相同的程序:(发布在下面)http://pastie.org/5081533,但到目前为止还没有成功。我是指针新手,不知道我做错了什么。任何帮助是极大的赞赏。

#define MAX 20

typedef struct temp 
{
int data;

char name[MAX];
char telephone[10];
char email[MAX];
char address[MAX];
char zipcode[10];

temp *next;
} node;


node* creation1 ( )
{    
  node *start= NULL;
  node *NEW = NULL;

  node  *current = NULL, *aux = NULL, *save = NULL; 

  NEW = (node*)malloc(sizeof(node)); 
  current = start= aux = save = NEW;

 return NEW;
}

node* creation2 ()
 {    
 node *start= NULL;
 node *NEW = creation1();
 start= NEW;

  return start;
}

node* creation3 ( )
{    

node *NEW = creation1();
node *current = NULL;
current=NEW;

   return current;
   }

   node* consult ()
   {

    node *NEW= creation1();

    node *start= creation2();

    node *current = creation3();




int exit;
printf("How many contacts do you want to add in the agenda? ");
scanf("%i",&exit);


for(int i=1; i<=exit; i++)
{

    NEW = (node*)malloc(sizeof(node));
    current->next=NEW;                 
    current = NEW; 
    fflush(stdin);
    puts("NAME: ");
    gets(NEW->name); 
    puts("TELEPHONE: ");
    gets(NEW->telephone);
    puts("EMAIL: "); 
    gets(NEW->email);
    puts("ADDRESS: ");
    gets(NEW->address);
    puts("ZIP CODE: ");
    gets(NEW->zipcode);
    NEW->next=NULL;


} 

  current = start->next;

  return current;

  }


node* order ()
{    

   node *NEW=creation1();
   node *start=creation2 ();
   node *current =NULL;
   current=consult();
   node *aux = NULL;
   node *save =  NULL;
   aux=NEW;
   save=NEW;



int i = 0;
do 
{
    i++;
    current = current->next; /* THIS IS WHERE I'M GETTING AN ERROR MS Visual Studio tells me: "Unhandled exception...Access violation reading location..." */
}
while (current != NULL);

current = start->next;
aux = current->next;

for (int j = 1; j < i; j++)
{

   current = start->next;
    aux = current->next;

    while(current->next != NULL)
    {
        if (strcmp(current->name,aux->name) > 0)
        {
            strcpy(save->name, current->name);
            strcpy(save->telephone, current->telephone);
            strcpy(save->email, current->email);
            strcpy(save->address, current->address);
            strcpy(save->zipcode, current->zipcode);

            strcpy(current->name, aux->name);
            strcpy(current->telephone, aux->telephone);
            strcpy(current->email, aux->email);
            strcpy(current->address, aux->address);
            strcpy(current->zipcode, aux->zipcode);


            strcpy(aux->name, save->name);
            strcpy(aux->telephone, save->telephone);
            strcpy(aux->email, save->email);    
            strcpy(aux->address, save->address);
            strcpy(aux->zipcode, save->zipcode);
        }
         current = current->next;
         aux = current->next;
    }  
}  

  return current;

}


    node* displayorder()
    {  
      node *NEW=creation1();
      node *start=creation2 ();
      node *current = order();

      current = start->next;


    while(current != NULL)
    {

    printf("\n********************");
    printf("\n NAME: %s",current->name);
    printf("\n TELEPHONE: %s", current->telephone);
    printf("\n E-MAIL: %s", current->email);
    printf("\n ADDRESS: %s ", current->address);
    printf("\n ZIP CODE: %s ", current->zipcode);
    current = current->next;
    }
    getch();


  return current;


 }



 node* displaysearch()
{    

node *current = displayorder();
node *start= creation2();

char search[MAX];

printf("\n\nGive a name to search: ");
scanf("%s",search);




current = start->next;
 while(current != NULL)
 {
  if(strcmp(search, current->name)==0)
  {
    printf("\n********************");
    printf("\n NAME: %s",current->name);
    printf("\n TELEPHONE: %s", current->telephone);
    printf("\n E-MAIL: %s", current->email);
    printf("\n ADDRESS: %s ", current->address);
    printf("\n ZIP CODE: %s ", current->zipcode);

}
     current = current->next;
  }

      return current;


getch();

}


int main(int argc, char** argv)
{  


  displaysearch();


}
4

1 回答 1

1

函数中的局部变量current从函数中order()获取其值consult()。该函数反过来返回start->next从未初始化为指向任何node结构的值。因此,内存访问冲突异常是预期的异常。

于 2012-10-19T02:52:11.120 回答