Segmentation Fault help!

This is a discussion on Segmentation Fault help! within the C Programming forums, part of the General Programming Boards category; Hey I'm working on a school project and I keep getting a segmentation fault error, which i've narrowed down (using ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    5

    Segmentation Fault help!

    Hey I'm working on a school project and I keep getting a segmentation fault error, which i've narrowed down (using printf) to the line bolded/underlined below.

    It was working until I put in the function prototype void read_elements, and now I get and error whenever getchar() is used.

    I am not including the entire program, as it is really long and I also dont want to get in trouble if a classmate decides to copy it all lol
    Please help!




    Code:
                                                                       
                                                 
    /* Homework */
    
    /* File: hw.c
     * Purpose: 
     * Author: Jonathon Taylor
     * Date: February 1, 2010
     */
    
    /* Preprocessor Directives */
    #include <stdio.h>
    #include <float.h>
    
    #define FILE_NAME "table.txt"
    
    
    
    /*----------------------------------------------------------------*/
    
    //Defining part as a type of structure
    
    typedef struct{
    char name[15];
    int number;
    char symbol[5];
    double weight;
    float group;
    int period;
    char type[25];
    char abbr[4];
    } Part;
    
    //Begin Central Coding
    
    void read_elements(Part *part1, FILE *fp, char sym[]);
    
    int main(void)
    {
    
    printf("\n\t\t****Periodic Table of Elements****\n\n");
    
    int i;
    char sym[5];
    char req;
    float gp;
    
    //Defining part variable from typedef (part)
    Part *part1,partm;
    
    part1 = &partm;
    
    FILE *fp;
    fp = fopen(FILE_NAME, "r");
    
    printf("Enter element symbol: ");
    
    //Read in symbol request
    
    i=0;
    
    req = getchar();						//Segmentation fault occurs on this line
    
    while(req != '\n')
    {
    sym[i] = req;
    i++;
    req = getchar(); 
    }
    
    
    sym[i] = '?';
    
    //Reseting variable
    
    i=0;
    
    //Skipping title
    
      for(i=0;i<=59;i++)
      {
      req = fgetc(fp); 
      }
      i=0;
    
    read_elements(part1,fp,sym);
    
     return 0;
    }
    
    
    
    
    
    void read_elements(Part *part1, FILE *fp, char sym[])
    {
    
    int i,n,g;
    char inp;
    n=0;
    double gp;
                                                                   //MASTER WHILE LOOP GOES HERE//
    while(n == 0)
    {
    
    
    		//Reading in element
    
    inp = fgetc(fp);
    while(inp !=',')
    {
    (*part1).name[i] = inp;
    inp = fgetc(fp);
    i++;
    }
    (*part1).name[i] = '?';
    i=0;
    		//Reading in number
    fscanf(fp,"%d", &(*part1).number);
    inp = fgetc(fp);
    
    
    	 	//Reading in symbol
    inp = fgetc(fp);
    i=0;
    while(inp !=',')
    {
    (*part1).symbol[i] = inp;
    inp = fgetc(fp);
    i++;
    
    }
    (*part1).symbol[i] = '?';
    i=0;
    		//Reading in weight
    fscanf(fp,"%lf", &(*part1).weight);
    inp = fgetc(fp);
    
    		//Reading in group
    
    fscanf(fp,"%f", &(*part1).group);
    inp = fgetc(fp);
    
    		//Reading in period
    fscanf(fp,"%d", &(*part1).period);
    inp = fgetc(fp);
    inp = fgetc(fp);
    
    
    
    		//Reading in type
    while(inp !=',')
    {
    (*part1).type[i] = inp;
    inp = fgetc(fp);
    i++;
    }
    (*part1).type[i] = '?';
    i=0;
    inp = fgetc(fp);
    
    		//Reading in abbreviation
    while(inp !='\n')
    {
    (*part1).abbr[i] = inp;
    inp = fgetc(fp);
    i++;
    }
    (*part1).abbr[i] = '?';
    i=0;
    
    						//End READING IN area of while loop
    						//Now TESTING FOR MATCH TO REQUESTED SYMBOL
    
    
     if (sym[i] == (*part1).symbol[i])	
     {
      while(sym[i]==(*part1).symbol[i])
     {
       if(sym[i] == '?')
    {
       i=0; 
     
     
    
    	//Printing Element
    
      printf("\nElement:\t");
         while((*part1).name[i] != '?')
      {
       printf("%c",(*part1).name[i]);
       i++;
      }
        i=0;
       
    
    	//Printing number
    
       printf("\nNumber:\t\t%d", (*part1).number);
    
    
    	//Printing symbol
     	
    printf("\nSymbol:\t\t");
        i=0;
     while((*part1).symbol[i] != '?')
      {
       printf("%c",(*part1).symbol[i]);
       i++;
      }
        i=0;
    
    	//Printing weight
    
    g = (*part1).weight;
    gp = (*part1).weight;
    if( gp == g)
     {
     
       if(g <= 0)
            {
        g = g * (-1);
        printf("\nAtomic Weight:\t(%d)",g);
            }
            else
            {
         printf("\nAtomic Weight:\t%d",g);
            }
    
     }
    else{
    	if((*part1).weight <= 0)
            {
        (*part1).weight = (*part1).weight * (-1);
        printf("\nAtomic Weight:\t(%f)",(*part1).weight);
    	}
    	else
    	{
         printf("\nAtomic Weight:\t%f",(*part1).weight);
    	}
        }
    	//Printing Group
    g = (*part1).group;
    gp = (*part1).group;
    if( gp != g)
     {
        printf("\nGroup:\t\t%.2f", (*part1).group);
     }
     else
     {
      printf("\nGroup:\t\t%d",g);
     }
     
    	//Printing Period
        printf("\nPeriod:\t\t%d", (*part1).period);
    
    	//Printing Type
    
        printf("\nType:\t\t");
         while((*part1).type[i] != '?')
      {
       printf("%c",(*part1).type[i]);
       i++;
      }
        i=0;
    
    	//Printing Abbr
    
        printf("\nType Abbr:\t");
         while((*part1).abbr[i] != '?')
      {
       printf("%c",(*part1).abbr[i]);
       i++;
      }
        printf("\n");
        i=0;
    
     n=1;
     break;
      
     }
    i++;		 
    
    }
    
    
    
    						//END MASTER WHILE LOOP
    }
    }
    }
    Last edited by jonktay; 05-21-2010 at 12:57 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    getchar returns an int, not a char. That may not explain the seg fault tho.

    Also, if you are using printf() without fflush(stdout) your debugging method may be inaccurate. Stdout is buffered, meaning there may be data in it that has not been displayed when the program crashes. A better alternative is
    Code:
    fprintf(stderr,
    since stderr is not buffered.

    Use code tags next time please!

    << !! Posting Code? Read this First !! >>
    Last edited by MK27; 05-21-2010 at 12:55 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You appear to have bolded and underlined the wrong line; you meant to highlight
    Code:
    sym[i] = req;
    instead. You only have room to store five things in sym, and yet....

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Fix your post so that the code is in CODE tags.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    5
    Quote Originally Posted by MK27 View Post
    getchar returns an int, not a char. That may not explain the seg fault tho.

    Also, if you are using printf() without fflush(stdout) your debugging method may be inaccurate. Stdout is buffered, meaning there may be data in it that has not been displayed when the program crashes. A better alternative is
    Code:
    fprintf(stderr,
    since stderr is not buffered.

    Use code tags next time please!

    << !! Posting Code? Read this First !! >>
    I'm not 100% sure on what you mean, but I think you might have it


    btw I forgot to say that the user input is gonna be a periodic symbol (i.e. 'H' or 'Au')


    how do you use the fprintf(stderr) ? cuz i feel like that could definitely help me if i'm looking at the wrong line of code as the error



    Quote Originally Posted by tabstop View Post
    You appear to have bolded and underlined the wrong line; you meant to highlight
    Code:
    sym[i] = req;
    instead. You only have room to store five things in sym, and yet....
    yeah, the input will be a max of 3 though so i dont think this will create an error, thanks tho









    and my bad about the code tags, i'm a newbie
    Last edited by jonktay; 05-21-2010 at 01:03 PM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If your printf looks like this:
    Code:
    printf("%d", that_pesky_x);
    use
    Code:
    fprintf(stderr, "%d", that_pesky_x);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    5
    its lookin like the error is definitely on the getchar() command

    the confusing part is that it worked perfectly before i inserted the function prototype

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Moved to C Programming forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jonktay View Post
    its lookin like the error is definitely on the getchar() command

    the confusing part is that it worked perfectly before i inserted the function prototype
    Because the real fault is in that function:

    Code:
    void read_elements(Part *part1, FILE *fp, char sym[])
    {
    
    int i,n,g;
    char inp;
    n=0;
    double gp;
                                                                   //MASTER WHILE LOOP GOES HERE//
    while(n == 0)
    {
    
    
    		//Reading in element
    
    inp = fgetc(fp);
    while(inp !=',')
    {
    (*part1).name[i] = inp;
    i is uninitialized!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    5
    I'm going to apologize in advance for all the questions to come, and thank you for your help or i'd be COMPLETELY lost right now lol


    ..
    so how would one go about fixing this error?

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    What value should i have right there? Zero? Then initialize it to 0.

    BTW, it's more normal to use "indirect" notation with pointers:
    Code:
    part1->name[i] = inp;
    rather than dereferencing with (*part1).
    Last edited by MK27; 05-21-2010 at 01:20 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    5
    WOOOOOOOOOOOOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

    omg, that was it

    i've been trying to fix this for like 6 hours lol, didnt even think to look there

    thanks!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06: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, 01:53 PM

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