Thread: Linked List and Array of Pointers

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    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.


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

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I think what you mean to do was this:
    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]; REMOVED THIS LINE
    	  for (k=0; k<40; k++)
                 ptr = Layout[i][j].Array[k];  // ADDED THIS LINE
    	    {
    	      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++;  REMOVED THIS LINE
    	    }
            }
        }
    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;
        }
      
    }
    I changed 3 lines of code, which I commented in the source.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    Thanks bithub

    Hey bithub,
    Thanks a lot. It seems to be workng now without giving me an error.
    Shall be back with some more questions as they come along.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    New error with Linked List Part 2

    Hi,
    For the second part of the code I need to fill in the Linked List with values for the line_used from a file. The file has the following format:

    Array size: 17 x 17 logic blocks.

    Routing:

    Net 0 (i_63_)

    SOURCE (18,1) Pad: 0
    OPIN (18,1) Pad: 0
    CHANY (17,1) Track: 2
    CHANX (17,1) Track: 3
    IPIN (17,1) Pin: 2
    SINK (17,1) Class: 0
    CHANX (17,1) Track: 2
    CHANY (16,2) Track: 4
    IPIN (16,2) Pin: 3
    SINK (16,2) Class: 0


    Hence I need to read the Channel X or Y and the coordinates and the corresponding track number. Depending on the Channel (X or Y ) and the coordinates I have to fill up the linked list.
    The logic is illlustrated in my code but when I run the program on VC++ I get an error message that the exe file has encountered an error. When I try to debug it says that it cannot evaluate the line_used expression and the temp=temp->nextptr expression.

    Also further I get the following errors on debugging

    Loaded 'C:\WINDOWS\system32\ntdll.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\version.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\msvcrt.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\advapi32.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\rpcrt4.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\user32.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\gdi32.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\shlwapi.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\apphelp.dll', no matching symbolic information found.
    The thread 0x7E8 has exited with code 0 (0x0).
    The thread 0xF88 has exited with code -1073741819 (0xC0000005).


    On GCC I simply get a Segmentation Fault.

    I am again all confused where to try looking for tthe problem.
    I tried printing out the tracknumber and coordinates also. They do print if I comment the part from **HERE** to **HERE**

    I am attaching the code 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 x1=0,x2=0,y1=0,y2=0,trackno1=0,trackno2=0,m,n;
      FILE *fp;
      SWITCH *temp;
      char ch;
      
     
    fp=fopen("e64.r","r");
      while(!feof(fp))
        { 
       **HERE**
          if (x1==x2)
            {
              if (y1==y2)
                { 
                  temp=Layout[17-y1][x1].Array[19+trackno2];
                  temp=temp->nextptr;
                  temp->line_used=1; 
                           
                  temp=Layout[17-y1][x1].Array[19-trackno1];
                  temp=temp->nextptr; 
                  temp=temp->nextptr; 
                  temp->line_used=1; 
                } 
                         
              else   
                {
                  if (y2==y1+1)
                    {
                      temp=Layout[17-y1][x1].Array[19-trackno1];
                      temp->line_used=1;
                 
                      temp=Layout[17-y1][x1].Array[9-trackno2];
                      temp=temp->nextptr;
                      temp->line_used=1; 
                    } 
                  else   
                    {
                      if (y2 > y1+1) 
                        { 
                          temp=Layout[17-(y2-1)][x1].Array[19+trackno2];
                          temp->line_used=1;
                   
                          temp=Layout[17-(y2-1)][x1].Array[9-trackno2];
                          temp=temp->nextptr;
                          temp=temp->nextptr;
                          temp->line_used=1;
                        }
                    }
                }
            } 
          else 
            { 
              if (y1==y2) 
                {
                  if (x1==x2+1) 
                    { 
                      temp=Layout[17-y2][x2].Array[19+trackno2];
                      temp=temp->nextptr;
                      temp=temp->nextptr;
                      temp=temp->nextptr;
                      temp->line_used=1;
                          
                      temp=Layout[17-y2][x2].Array[29+trackno1]; 
                      temp=temp->nextptr;   
                      temp=temp->nextptr;
                      temp->line_used=1; 
                    } 
                  else 
                    { 
                      if (x1 > x2+1)
                        {
                          temp=Layout[17-y1][x1-1].Array[19-trackno1];
                          temp=temp->nextptr;
                          temp=temp->nextptr;
                          temp=temp->nextptr;
                          temp->line_used=1;
                 
                          temp=Layout[17-y1][x1-1].Array[29+trackno1];
                          temp=temp->nextptr;
                          temp->line_used=1; 
                        } 
                    } 
                } 
              else 
                {         
                  if (y2==y1+1) 
                    { 
                      temp=Layout[17-y1][x2].Array[9-trackno2];
                      temp=temp->nextptr;
                      temp=temp->nextptr;
                      temp=temp->nextptr;
                      temp->line_used=1;
                       
                      temp=Layout[17-y1][x2].Array[29+trackno1];
                      temp->line_used=1; 
                    } 
                }
    			  
            } **HERE**
    	  
    
    		
                           
                 
          ch=fgetc(fp); 
          if (ch=='S') 
            { 
              x1 = 0; 
              y1 = 0; 
              x2 = 0;
              y2 = 0;
                          
              ch=fgetc(fp); 
              if(ch=='C')
                { 
                  ch=fgetc(fp); 
                  if(ch=='H') 
                    { 
                      ch=fgetc(fp); 
                      if(ch=='A')
                        { 
                          ch=fgetc(fp);  
                          if(ch=='N')
                            {
                              ch=fgetc(fp);
                              if(ch=='X')
                                {
                                  ch=fgetc(fp);//the blank
                                  //ch=fgetc(fp);//the colon
                                  ch=fgetc(fp);//the (
                                  fscanf(fp,"%d",&x1);
                                  printf("%d\n",x1); 
                                  ch=fgetc(fp);//the ,
                                  fscanf(fp,"%d",&y1); 
                                  printf("%d\n",y1); 
                                  for(m=0;m<10;m++)//no of charac till u reach track no
                                    ch=fgetc(fp);
                                  fscanf(fp,"%d",&trackno1); 
                                  printf("go\n");
                                  printf("%d\n",trackno1);
                         
                       
                                  //store these in the linked list
                                }
                              else if (ch=='Y')
                                {
                                  ch=fgetc(fp);//the blank
                                  //ch=fgetc(fp);//the colon
                                  ch=fgetc(fp);//the (
                                  fscanf(fp,"%d",&x2);
                                  printf("%d\n",x2);
                                  ch=fgetc(fp);//the ,
                                  fscanf(fp,"%d",&y2);
                                  printf("%d\n",y2);
                                  for(n=0;n<10;n++)//no of charac till u reach track no
                                    ch=fgetc(fp); 
                                  fscanf(fp,"%d",&trackno2);
                                  printf("%d\n",trackno2);
                                   
                                     
                                  //store these in the linked list
                                } 
                            } 
                        }
                    }  
                } 
            } 
                               
        } 
      fclose(fp); 
                                   
      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;      
        } 
      
    }

    I am open to any help as I can get regarding this.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop making new threads for the same topic. Yes, this is your origional one. No, you do not double post, or bump your own threads. Go read the forum guidelines.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  4. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM
  5. Replies: 5
    Last Post: 11-20-2001, 12:48 PM