Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 3rd, 2005, 6:15 PM   #1
Nish
Newbie
 
Join Date: Mar 2005
Posts: 5
Rep Power: 0 Nish is on a distinguished road
Question Linked List and Array of POinters

Hey guys. I need to write a program that initializes a switch matrix type of structure. The switch matrix structure is like this.
The switch matrix consists of 10 pins up 10 to the left 10 to the right and 10 to the bottom. These pins can be numbered from 0 to 39.
Now pin 0 connects to pin 19 and 29 and 39. Similarly pin 1 connects to pin18, 28, 38.
I have found out a general form in which the pins are connected illustrated in the code. Now how I have modeled this is using a Linked List which consists of 4 elements. Each element consists of 2 variables pin_no and line_used. The pin_no field is set to the value of the pin to which it connects for eg for pin 0 my first linked list element will have pin_no as 0 then the next element as 19 and 29 and so on..while the line_used is set to 0 for all cases and 2 for the pin itself i.e. for pin 0 the line_used bit for ther first element is 2 and for the remaining three it is 0 and so on.
Now for the 40 pins I do this again so as to have 4 linked list elements 40 times. Hence I make an array of 40 pointes each element of the array pointing to the head of the linked list.
Also I need to have a 2D array of 18 by 18 elements each element of the aarray being a switch matrix.
However when I am rumming the program I get a segmentation fault (core dumped). I dont know where to look for errors. Could you suggest.

Below is the code:

#include <stdio.h>  
#include <stdlib.h> 

struct switch_matrix  
{
  
  int pin_no;             
  int line_used;
  struct switch_matrix *nextptr;
  
};

struct switch_block
{
  
  struct switch_matrix *Array[40];
  
}Layout[18][18];


typedef struct switch_matrix SWITCH;
void build_univ_switch(struct switch_block *X);






int main()
{
  int k; 
  
  SWITCH *ptr;
  struct switch_matrix *Array[40];

  for(i=0; i<18; i++)
    {
      for(j=0; j<18; j++)
	{
	  build_univ_switch(&Layout[i][j]);

	  /* DEFINE SWITCH CONNECTIONS FOR UNIVERSAL SWITCH*/

	  ptr = Layout[i][j].Array[0];
	  for (k=0; k<40; k++)
	    {
	      if (k<=9)  
	      {
	      	ptr->pin_no = (k);
		ptr->line_used = 2;
		ptr = ptr->nextptr;
		ptr->pin_no = (19 - k);
		ptr = ptr->nextptr;
		ptr->pin_no = (29 - k);
	      	ptr = ptr->nextptr;
	      	ptr->pin_no = (39 - k);
	      }	
	      else 
		if (k>9 && k<=19)  
		  {
		    ptr->pin_no = (19 - k);
		    ptr = ptr->nextptr;
		    ptr->pin_no = (k);
		    ptr->line_used = 2;
		    ptr = ptr->nextptr;
		    ptr->pin_no = (39 - k);
		    ptr = ptr->nextptr;
		    ptr->pin_no = (49 - k);
		  }
		else 
		  if (k>19 && k<=29) 
		    {
		      ptr->pin_no = (29 - k);
		      ptr = ptr->nextptr;
		      ptr->pin_no = (39 - k);
		      ptr = ptr->nextptr;
		      ptr->pin_no = (k);
		      ptr->line_used = 2;
		      ptr = ptr->nextptr;
		      ptr->pin_no = (59 - k);
		    }
		  else
		    if (k>29 && k<=39) 
		      {
			ptr->pin_no = (39 - k);
			ptr = ptr->nextptr;
			ptr->pin_no = (49 - k);
			ptr = ptr->nextptr;
			ptr->pin_no = (59 - k);
			ptr = ptr->nextptr;
			ptr->pin_no = (k);
			ptr->line_used = 2;
		      }
	      ptr++;
	    }
        }
    }
return 0;
}
  
   



void build_univ_switch(struct switch_block *X)
{ 
  SWITCH *head;
  SWITCH *currentptr;
  int i, j;
  
  
  

  for (i = 0; i<40; i++)
    {
      
      /*BUILDS LINKED LIST*/
      head = NULL;
      for (j = 1; j<=4; j++)
	{
	  currentptr = (SWITCH *) malloc (sizeof (SWITCH));
	  currentptr->line_used = 0;
	  currentptr->nextptr = head;
	  head = currentptr;
	}
      //currentptr = head;
      X->Array[i] = head;
    }
  
}

Last edited by Nish; Mar 3rd, 2005 at 6:36 PM. Reason: I forgot the 2D arrray
Nish is offline   Reply With Quote
Old Mar 3rd, 2005, 10:09 PM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
try compiling using the -g flag, and running gdb, or if you're in windows, try using the MS debugger. it'll give you a better idea where you're going wrong. In a program of this level, it's tough to be able to see what's going on, and using a debugger is a great way to find where your error lies.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:24 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC