Thread: Segmentation Fault, very early on.

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    1

    Segmentation Fault, very early on.

    Getting a segmentation fault extremely early on in the program when I was looking for debugging.

    Are there any obvious mistakes? Pretty new to C so far...


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct
    {
      int i1;
      int i2;
      double d;
      char n[50];
    } student;
    
    int main()
    {
      printf("Print");
      FILE *in = fopen("/home/lolz/pd03", "r");
      int max = 100;
    
      student ** s = malloc(max * sizeof(student*));
      student * temp = malloc(sizeof(student));
    
      int i;
      for (i = -1; i < max && !feof(in); i++)
      {
        fscanf(in, "%d %d %lf %[^\n]", &temp->i1, &temp->i2, &temp->d, temp->n);
        s[i] = temp;
        temp = malloc(sizeof(student));
      }
      fclose(in);
    
      student *s_temp;
      int current;
      int index;
      int high;
      int comp;
      for(current = 0; current < i; current++)
      {
        high = current;
        
        for(index = current + 1; index < i; index++)
        {
          if(strcmp(s[high]->n, s[current]->n) > 0)
          high = index;
        }
    
        s_temp = s[high];
        s[high] = s[index];
        s[index] = s_temp;
      }
    
      int j;
      for(j = 0; j <= i; j++)
      {
        printf("%2d, %5d, %3d, %7.2lf, %s \n", j+1, s[j]->i1, s[j]->i2, s[j]->d, &s[j]->n);
      }
         
      return 0;
    }

    pd03 is...

    Code:
    6383 21 13.77 Andrew Murarik
    6335  6 10.92 Melissa Zahurak
    4362 27 20.90 Andrew Zahurak
    6926 20 16.26 Chris McGettigan
    5211 13 23.67 Charles Glotfelty
    1530 22 17.23 Evan Brown
    4929 32 26.22 Joseph Crowley
    8167 33 14.56 Ryan Mattern
    3229 23 18.21 Melissa Reed
    8537 28 29.24 Andrew Hartz
    4413  6  6.91 Dayne Bruce
    1873 37 13.70 Laura Bagi
    8305  5  8.84 Justin Crowley
    8505 21  7.29 Justin Earon
    3124  5 17.82 Andres Getgen
    9367 34 13.64 Janine McGettigan
    2087 38 26.76 Daniel Bagi
    5584 38  8.51 Joseph Hinaman
    2932 20 22.76 Joseph Bagi
    8012 21  7.86 Laura Sisilli
    7795 15 16.34 Daniel Hinaman
    6601 37 15.02 Bradley Murarik
    4652 16 27.01 Evan McGettigan
    8619 45 13.29 Carl Allhiser
    9097 16  6.81 Ryan Seyler
    5927 17  8.56 Jacob Reed
    6586 10  7.06 Jhosep Murarik
    9624 33 22.71 Cody Hinaman
    5503 34 20.70 Jason Gatti
    8715 45 11.49 Daniel Bruce
    9618 15 10.46 Janine Hinaman
    6555 34 12.88 Cody Zahurak
    7841 10 25.93 Cody Bagi
    3914 37 12.56 Janine Mendoza
    7227 45 16.59 Chris Rosa
    1551 22  6.28 Jaclyn Getgen
    7474 21 22.58 Andrew Bagi
    7793 28 22.28 Carl Gatti
    6928 14 25.76 Daniel Perron
    1763 13 15.38 Jocelyn Getgen
    4904 18 29.28 Dayne Hartz
    2917  7 27.96 Thomas Hartz
    5470 43 14.90 Charles Rosa
    7725 24  9.90 Andres McGettigan
    3954 21 18.69 Chris Bruce
    4464 12 17.07 Justin Perron
    6348 46 22.22 Joseph White
    3343 36  9.68 Jocelyn Bagi
    4311 15 10.05 Charles Crowley
    6730 48 15.05 Jason Mendoza

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you really think you can assign something to s[-1]?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM