Thread: Data struct question

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    Data struct question

    I'm writing a program which organizes a buses network.
    these are the structs I use:
    Code:
    typedef struct lns{
    	int num;
    	struct lns *nextline;
    }lns;
    
    typedef struct stns{
    	int code;
    	struct stns *nextstn;
    }stns;
    
    
    typedef struct {
    	int num;
    	stns *stations;
    	
    }LINE;
    
    typedef struct {
    	int SNcode;
    	int CTcode;
    	char city[30];
    	char adress[30];
    	lns *lines;
    }STATION;
    I have some functions that fill some data in structures.
    Now, I got a function which ask for a station code & determine which lines pass this station.

    Code:
    FindLines (LINE *L)
    {
    	int i=0, orgn, dest;
    	puts ("\nEnter origin station code first and destination station code second");
    	flushall();
    	scanf ("%d %d", &orgn, &dest);
    		while (L[i].stations->code && i<STN)
    		{
    			if (orgn==L[i].stations->code)
    			{
    				L[i].stations=L[i].stations->nextstn;
    				while (L[i].stations->code)
    					{
    						if (dest==L[i].stations->code)
    						{
    							printf ("line %d pass origin code %d station & destination code %d station", L->num, orgn, dest);
    							break;
    						}
    						else 
    							puts ("No match found");
    					}
    			}
    		L[i].stations=L[i].stations->nextstn;
    		i++;
    		}
    }
    When I run it I get infenite output of "No match found", though my input has positive data.
    Debugger shows I got stuck in last iteration of the top while loop, I think, with a memory allocation error.
    Help will be highly epriciated!

    Ronen

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while (L[i].stations->code)
    Since nothing inside the loop changes this, it is a constant and therefore true all the time.

    Set a temporary pointer, and follow the list with something like
    temp = temp->nextstn;
    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
    Jun 2004
    Posts
    123
    This is my modified function, as I understood your offer:
    Code:
    FindLines (LINE *L)
    {
    	int i=0, orgn, dest;
    	LINE *temp;
    	temp=L;
    	puts ("\nEnter origin station code first and destination station code second");
    	flushall();
    	scanf ("%d %d", &orgn, &dest);
    		while (temp[i].stations->nextstn && i<STN)
    		{
    			if (orgn==temp[i].stations->code)
    			{
    			
    				while (temp[i].stations->nextstn)
    					{
    						temp[i].stations=temp[i].stations->nextstn;
    						if (dest==temp[i].stations->code)
    						{
    							printf ("line %d pass origin code %d station & destination code %d station", temp->num, orgn, dest);
    							break;
    						}
    						else 
    							puts ("No match found");
    					}
    			}
    		L[i].stations=L[i].stations->nextstn;
    		i++;
    		}
    }
    I'm still stuck in the same place with same responds:
    Unhandled exception in "filename" access violation

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    temp = line;
    while ( temp ) {
      // some stuff
      temp = temp->next;
    }
    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.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    only when I put
    >while (i<STN)
    in first loop, can function work.
    didn't manage to find any other solution to it.

    Code:
    FindLines (LINE *L)
    {
    	int i=0, orgn, dest;
    	LINE *temp;
    	temp=L;
    	puts ("\nEnter origin station code first and destination station code second");
    	flushall();
    	scanf ("%d %d", &orgn, &dest);
    		while (i<STN)
    		{
    			if (orgn==temp[i].stations->code)
    			{
    			
    				while (temp[i].stations->nextstn)
    					{
    						temp[i].stations=temp[i].stations->nextstn;
    						if (dest==temp[i].stations->code)
    						{
    							printf ("line %d pass origin code %d station & destination code %d station", temp->num, orgn, dest);
    							break;
    						}
    						else 
    							puts ("No match found");
    					}
    			}
    		temp[i].stations=temp[i].stations->nextstn;
    		i++;
    		}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:42 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM