Problem with structures and seg fault

This is a discussion on Problem with structures and seg fault within the C Programming forums, part of the General Programming Boards category; I have some code and I keep getting segfaults, I have found out here the error is happening and it ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    13

    Question Problem with structures and seg fault

    I have some code and I keep getting segfaults, I have found out here the error is happening and it happens when I am trying to assign values to a structure. I thought I had done it right but apparently not. Here is my code and I put comments were the error is happening in the fillStruct function.

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    struct NODE //list node
    {
    
     char destination;
     struct NODE* next;
    
    };
    
    struct LIST  //linked list
    {
     char startingPoint;
     struct NODE* first;
    
    };
    
    void fillStruct(char* fileName, struct LIST map[]);
    void printList(struct LIST map[]);
    void depthSearch(struct LIST map[]);
    void breadth(struct LIST map[]);
    void topoSort(struct LIST map[]);
    struct NODE* ListIterator(struct NODE* start);
    
    
    
    void fillStruct(char* fileName, struct LIST map[]){
    
    char points[60];
    FILE* fpIn;
    int i=0;
    char readIn=' ';
    int j=0;
    struct NODE* temp;
    
    for(j=0;j<60;j++)
    points[j]=' ';
    
    if((fpIn=fopen(fileName,"r"))!=NULL)
    do{
    
    readIn=getc(fpIn);
    if(readIn!=EOF && readIn>='A')
    points[i++]=readIn;
    
    }while(readIn<'A' && readIn!=EOF);
    
    for(j=0;points[j]!=' ';j+=2){
    
    if(map[(int)points[j]-65].startingPoint==' ')
    map[(int)points[j]-65].startingPoint=points[j];
    
    temp->destination=points[j+1]; //error happens here
    temp->next=NULL;                     // and here
    
    if(map[(int)points[j]-65].first==NULL)
    map[(int)points[j]-65].first=temp;
    else{
    ListIterator(map[(int)points[j]-65].first)->next=temp;
    }
    
    
    }//end for loop
    
    
    
    }
    any help is greatly appreciated, Thanks.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Code:
    struct NODE* temp;
    That's a pointer. A pointer which does not point to anything of value...you don't know where it points. Before you use a pointer it MUST point to a valid memory location. You do this either by pointing it at something already present in memory, or by allocating memory with malloc/calloc.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    temp doesn't exist, and yet you try to use it. You need to make temp point to actual memory that you own.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    13
    Thanks that fixed it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structures
    By kiros88 in forum C Programming
    Replies: 9
    Last Post: 09-16-2009, 03:12 PM
  2. Memory allocation for structures
    By Lynux-Penguin in forum C Programming
    Replies: 3
    Last Post: 04-28-2002, 03:33 PM

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