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.


The code is below:

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;
    }
  
}