Thread: strcmp on a linked list to a string n

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    17

    strcmp on a linked list to a string n

    I have a problem working this one out for strings... this small function should return a pointer to fred for example if fred were in the list or NULL otherwise, a simple find operation. The output always returns the same pointer no matter how I've tried different things.

    In short, is the logic of this method sound? Can anyone spot the problem? The correct header files are called and it compiles but just doesn't output the correct pointer ---> given a string n and a pointer to the list as parameters. The linked list has only 2 parts to the struct - data_item and next.


    Code:
    node_ptr find(char *n, node_ptr list)
    {
    	node_ptr located = list->next;
    	
    	while(located != NULL && (!strcmp(located->data_item, n) == 0));
    	{
    		located = located->next;
    	}
    
    	return located;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    The ; at the end of the while loop is a killer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    17
    sorry salem that ; shouldn't be there...

    without the ; ...

    I've noticed another issue with a previously working method not working at all so the problem may be elsewhere... does the find method look logical to you at this point? I am beginning to think my issue is somewhere else.

    any advice would be welcome

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    17
    no, I realised n had no string being passed in... but unfortunately its still returning the exact same pointer no matter what - it returns NULL if its not in there but otherwise it returns the same pointer every time...

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    17
    no i have found the issue is in main... I'll work on it tomorrow and try to see what happened. I have a bit of junk code that compiled - thanks for looking at it though, greatly appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM