Segmentation Fault on -> and strcpy line

This is a discussion on Segmentation Fault on -> and strcpy line within the C Programming forums, part of the General Programming Boards category; Hi people, Im a newbie in here so please bare with my lack of C knowledge. I'm currently trying to ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Post Segmentation Fault on -> and strcpy line

    Hi people,
    Im a newbie in here so please bare with my lack of C knowledge. I'm currently trying to read from a file and storing its information to an array of a structure. Obviously I'm getting an error (Segmentation fault). I know its the -> sign and the strcpy line but Im just clue less on how to fix it. The code is below.

    Code:
     void input_students(student_type * students[], int * n, FILE* ifp1){
    //declare variables here
            char last_name[26];
            //last_name = (char *) malloc( sizeof(char) );
            int std_no; // integer student number
            char  major[16]; //the major
            int num_of_courses; //the num of courses the person is taking
            char course_nums[8][7];
            int course_marks[8];
            *n=0;
            int i=0;
            int j;
            while (fscanf(ifp1, "%s", last_name) != EOF){
                    students[j]= (student_type*)malloc(sizeof(student_type));
                    
                    strcpy(*(students[j]->last_name), last_name);
    
                    fscanf(ifp1 , "%d %s %d", &students[j]->std_no, &students[j]->major,
                    &students[j]->num_of_courses);
    
                            for(i=0; i < students[j]->num_of_courses; i++){
                                fscanf(ifp1, "%s", &students[j]->course_nums[i]);
                                fscanf(ifp1, "%d", &students[j]->course_marks[i]);
    
    
                    } j++;
    
            }
       *n = j;
    }
    Any help would be great. Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    How did you declare student_type?

    Also, try writing all your structures and variables as true arrays (not pointers which need to be malloc'ed). Once you have it working, it's an easy change to replace an array with a pointer and a malloc (the rest of the code is unlikely to notice).
    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
    Apr 2005
    Posts
    2
    Here is my student_type

    Code:
    struct STUDENT{
            char * last_name[26];                    
            int std_no; // integer student number
            char * major[16]; //the students major
            int num_of_courses; //the number of courses
            char * course_nums[8][7]; //2d array holding 8 strings max, each up to 6 chars
            int * course_marks[8]; //integer array for the marks of each course
    };
    
    typedef struct STUDENT student_type;
    As for writing it as true arrays, I'm not quite understanding that part.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    Well for a start, you need to remove all those * from your structure.

    I presume you have something like this in your main()
    STUDENT students[100];

    Y'know what - post the whole lot.
    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
    Mar 2005
    Posts
    38
    You dereference 'students[j]->last_name' right after allocation of 'students[j]'. It probably contains garbage, which shouldn't be dereferenced.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    look at salme's post. he said the proper error in your code. u shouldn't deference the array in your case. and more over which gives u someother meaning. it should be something like this

    Code:
    struct STUDENT{
            char  last_name[26];                    
            int std_no; // integer student number
            char  major[16]; //the students major
            int num_of_courses; //the number of courses
            char course_nums[8][7]; //2d array holding 8 strings max, each up to 6 chars
            int  course_marks[8]; //integer array for the marks of each course
    };
    
    typedef struct STUDENT student_type;
    and int the function
    Code:
     strcpy(students[j]->last_name, last_name);
    hope this helps u a bit

    s.s.harish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcpy(), 2 strings, and segmantation fault
    By jigidyjensen in forum C Programming
    Replies: 4
    Last Post: 03-20-2009, 02:42 PM
  2. Segmentation Fault
    By blaksheep423 in forum C Programming
    Replies: 11
    Last Post: 12-07-2006, 05:28 AM
  3. Hangman Game - Rewrite (Segmentation Fault?)
    By tigrfire in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 07:56 PM
  4. Replies: 2
    Last Post: 10-29-2005, 07:05 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM

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