2012-10-19 55 views
0

我創建了以下程序http://pastie.org/5081517,它是一個鏈接列表(議程),按字母順序排列聯繫人並讓用戶搜索特定聯繫人。帶指針和函數的C列表

然後,我嘗試使用功能相同的程序:(張貼如下)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

請直接在您的文章中添加代碼,將其簡化爲展示您的問題的最小示例。在一行的開頭添加四個空格以創建一個代碼塊。 –

+0

這是很多代碼。有什麼問題?你對什麼感到困惑? – nneonneo

+0

嗯,我不知道我是否正確地聲明瞭函數並返回指針。程序到達order()函數的第一個循環後,我會收到錯誤。 – user1758027

回答

1

局部變量current在功能order()從功能consult()得到它的價值。該函數反過來返回從未初始化的start->next的值以指向任何node結構。因此,內存訪問衝突異常是預期的。

+0

Serge的回答是一個好的開始,但我強烈建議(與其他人一樣)儘量減少您發佈的代碼,並且您會找到所需的幫助。考慮用代碼的部分替換,說明該部分的功能。 –