Thread: loop problem

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    61

    loop problem

    Hi all!

    I don't know what's wrong with my code. The program behaves as if the if-memchr-line wouldn't exist. Perhaps there's a problem with passing the next struct (p) in the linked llist, while running the loop many times?

    Thanks in advance for help!

    Code:
    	while(!feof(fp))
    	{
    		fgets(buf, 750, fp);
    		if(memchr(buf, '~', 750))
    		{
    			if(head)
    			{
    				if(save==1)
    				{
    					Print(head);
    				}
    				Delete(head);
    			}
    			head = malloc(sizeof *head);
    			p=malloc(sizeof(struct node));
    			head->next=p;
    			save=0;
    		}
    		Build_linked_list(p);
    	}

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    After you fix the feof() problem, switch from memchr() to strchr(). The way you have it searches the entire 750 bytes of the array (I assume it's 750 bytes long) for a tilde; but unless you type a 750 character line, fgets() will store fewer than 750 bytes. There is almost no reason why, if you're reading a string, you wouldn't want to just search that string for a character. Otherwise you're searching garbage (or previously stored values) past the end of the newly-read string.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Thanks for the comments!

    The big mistake in my code seemed to be that I tried to build the beginning of the linked list in the main function and the tail in another function. Now I understand that the entire linked list should be built in one function... Should this be the main function or can it be built it in other functions as well.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sababa.sababa View Post
    Thanks for the comments!

    The big mistake in my code seemed to be that I tried to build the beginning of the linked list in the main function and the tail in another function. Now I understand that the entire linked list should be built in one function... Should this be the main function or can it be built it in other functions as well.
    You should pass the argument's(head's) address(call by reference), to make changes in the linked list. It doesn't depend on whether the function should be main() or any other.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    ...so instead of writing:
    Code:
    Build_linked_list(p);
    ,,,I should write:
    Code:
    Build_linked_list(p, p->next);
    ...and the called function should begin like this:
    Code:
    struct node* Build_linked_list( struct p, struct p->next )
    I hope I've understood it right...

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Now I understand...
    I've to pass the head and then wind up the linked list till the place where I wanna add something.

    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with infinite loop in signal handler
    By semun in forum C Programming
    Replies: 6
    Last Post: 07-22-2009, 01:15 PM
  2. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  3. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  4. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM