Data struct question

This is a discussion on Data struct question within the C Programming forums, part of the General Programming Boards category; I'm writing a program which organizes a buses network. these are the structs I use: Code: typedef struct lns{ int ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 11:42 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04: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, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21