Thursday, 8 August 2013

C Pointers & Linked Lists

C Pointers & Linked Lists

First post, so go easy :)
I am coding a simple C employee database program through the use of
(single) linked lists. At the moment I am trying to write the "delete
employee" function, which is given below.
I am trying to cycle through the linked list using the while loop, and
STOP when the current pointer name field matches the name the user wishes
to delete. (Stored in linestore.)
For some reason, it just keeps looping through the database till the end
no matter what. I have tried printing the contents of linestore &
currptr->name at each stage, and they appear correct, so I have no idea
what I am doing wrong.
Any help would be greatly appreciated.
Delete Employee function:
char *lineptr;
char linestore[300];
lineptr = &linestore;
struct Employee *currptr = root;
struct Employee *prevptr = NULL;
fprintf(stderr, "\nPlease enter the EXACT name of the employee to be
deleted.\n");
read_line(stdin, lineptr, MAX_NAME_LENGTH); //linestore function is
working (checked)
while ( (currptr->name != linestore) & (currptr != NULL) )
{
fprintf(stderr, "\n***Searching database...***\n");
fprintf(stderr, "***The current record is %s", currptr->name);
prevptr = currptr;
currptr = currptr->next;
}
if ( currptr->name == linestore )
{
fprintf(stderr, "\n***Record DELETED.***\n");
}

No comments:

Post a Comment