0

I have created the following program http://pastie.org/5081517 which is a linked list (agenda) that orders the contacts in alphabetical order and lets the user search for a specific contact.

I then tried to make the same program with functions: (posted below) http://pastie.org/5081533 but have been unsuccessful thus far. I am new to pointers and have no clue what I'm doing wrong. Any help is greatly appreciated.

#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
  • 4
    Please add the code directly in your post, stripped down to a minimal example that exhibits your problem. Add four spaces at the beginning of a line to create a code block. Commented Oct 19, 2012 at 1:58
  • That's a lot of code. What's the problem? What are you confused about? Commented Oct 19, 2012 at 2:19
  • Well I'm not sure if I'm declaring the functions correctly and also returning the pointers. I get an error as soon as the program gets to the first loop in the order() function. Commented Oct 19, 2012 at 2:23
  • Error in MS Visual Studio tells me: "Unhandled exception...Access violation reading location..." and it points to the current = current->next in: int i = 0; do { i++; current = current->next; } while (current != NULL); Commented Oct 19, 2012 at 2:28

1 Answer 1

1

The local variable current in the function order() get its value from the function consult(). That function in turn returns the value of start->next that is never initialized to point to any node structure. Thus the Memory Access Violation Exception is the expected one.

Sign up to request clarification or add additional context in comments.

1 Comment

Serge's answer is a good start, but I strongly suggest (as others have) you minimize the code you post, and you'll find the help you need. Consider replacing with sections of the code with comments that say what that section does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.