seg fault?

This is a discussion on seg fault? within the C Programming forums, part of the General Programming Boards category; im not sure as to why the program is seg faulting when im running it could someone tell me why ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    seg fault?

    im not sure as to why the program is seg faulting when im running it could someone tell me why n how to fix it?


    Code:
    #include "cal.h"
    
    /****************************************************************************
    * Function main() is the entry point for the program.
    ****************************************************************************/
    int main(void)
    {
       unsigned int month;
       unsigned long year;
       int MonthIndex;
       
       getMonth(&month);
       getYear(&year);
       GetDaysofMonth(&month, &year, MonthIndex);
       
       
       
       return EXIT_SUCCESS;
    }
    
    
    /****************************************************************************
    * Function getMonth() prompt the user for a number MIN_MONTH to MAX_MONTH and
    * returns that number. The number 0 is valid because this indicates that the
    * user wants to select all months.
    ****************************************************************************/
    unsigned getMonth(unsigned int  *month)
    {
       
       /*** declare variables*/
      
      int i;
      int flag = 0;
      int n = 0;
      char buff [BUFF_SIZE];
      int j;
      printf("Please enter a month between 0 - 12\n");
    
      
      
      
      
      do
      {     
            /* input month*/
            fgets(buff, BUFF_SIZE, stdin);
    	
            /** ascii to integer conversion**/
    	*month = atoi(buff); 
           
    	/* check for bad input*/
    	while(flag == 1 || *month <0 || *month >12)
    	{
    	   flag = 0;
    	   
    	   printf("**Wrong input Enter month from 0 - 12 only!**\n");
    	   
    	   
    	   fgets(buff, BUFF_SIZE, stdin);
    	   
    	   /*cast from ascii to unsigned int*/
    	   *month = atoi(buff); 
    	
    	}
         
    	printf("continue with program!\n");
           
           
       }
       while(flag != 0 );
            
            
           
    	
    	
        
         
         
         
     
      
      
       
       return EXIT_SUCCESS;
    
    }
    
    
    /****************************************************************************
    * Function getYear() prompts the user for a number MIN_YEAR to MAX_YEAR and
     GetDaysofMonth(month, year, MonthIndex);* returns that number.
    ****************************************************************************/
    unsigned getYear(unsigned long *year)
    {
        
       return EXIT_SUCCESS;
    }
    
    
    /****************************** GetDaysofMonth(month, year, MonthIndex);**********************************************
    * Function displayCalendar() displays the calendar for the user.
    * The function will display the calendar for a whole month.
    * If the user supplied a month of "0", then a calendar for a whole year
    * is displayed instead with each month displayed under the previous one (you
    * don't need to try to display months side by side).
    * Give attention to getting the output format exactly as shown below
    * (including headings and alignment). Here's an example for March 2006:
    * --------------------
    *      March 2006int *MonthIndex
    *  S  M Tu  W Th  F  S
    *           1  2  3  4
    *  5  6  7  8  9 10 11 
    * 12 13 14 15 16 17 18
    * 19 20 21 22 23 24 25
    * 26 27 28 29 30 31
    * -------------i-------
    ****************************************************************************/
    void displayCalendar(unsigned int month, unsigned long year)
    {
        
           
    
    
    
    
    }
    
    /****************************************************************************
    * Function readRestOfLine() is used for buffer clearing. Source: 
    * https://inside.cs.rmit.edu.au/~sdb/teaching/C-Prog/CourseDocuments/
    * FrequentlyAskedQuestions/
    ****************************************************************************/
    void readRestOfLine()
    {
       int c;
    
       /* Read until the end of the line or end-of-file. */   
       while ((c = fgetc(stdin)) != '\n' && c != EOF)
          ;
    
       /* Clear the error and end-of-file flags. */
       clearerr(stdin);
    }
    
    /*******************************************************************************
    *   A function to work out the day of the week for any date using the zella's algorith
    *
    *
    ********************************************************************************/
    
    unsigned GetDaysofMonth(unsigned  month, unsigned  year, int MonthIndex)
    {
        int arr[12]= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 30};
       /*
        if(year%4==0 && (year%100!=0 || year%400 ==0))
        {
            arr[1] = 29;
        
        }*/
        return arr[MonthIndex];
    
    }

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Next time, include the whole program. cal.h presumably has your function prototypes.

    Anyway, when I added my own prototypes based on your definitions:

    Code:
    bazz.c: In function 'main':
    bazz.c:18: warning: passing argument 1 of 'GetDaysofMonth' makes integer from pointer without a cast
    bazz.c:18: warning: passing argument 2 of 'GetDaysofMonth' makes integer from pointer without a cast
    That would be one large problem with it. Did your compiler not warn about this?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Only obvious things I've picked up;

    MonthIndex in main() is not initialised (meaning it's value is unpredictable), and GetDAysOfMonth() returns arr[MonthIndex].

    The first two arguments for MonthIndex are unsigned values; you're passing pointers.

    I'd guess that cal.h does not include declarations (prototypes) of the functions called by main(). Which means that, when the functions are called, the compiler treats them differently (eg an int return type) than the actual definitions that come later. If you had prototypes in the right place, the compiler would have complained bitterly and refused to build an executable, so you wouldn't have seen a seg fault.

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    yehhh it did ill show you my .h file
    Code:
    * Header files. */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    /* Constants. */
    #define MIN_MONTH 0
    #define MAX_MONTH 12
    #define MIN_YEAR 0
    #define MAX_YEAR 9999
    #define BUFF_SIZE 80
    #define FALSE 0
    #define TRUE  1
    /* Function prototypes. */
    unsigned getMonth();
    unsigned getYear();
    unsigned GetDaysofMonth();
    /*void displayCalendar(unsigned month, unsigned year);*/
    void readRestOfLine();

  5. #5
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    does anyone know where im goin wrong with my prototypes in my .h file compared with my .c file

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Your prototypes are incomplete, it should be, for example:
    Code:
    unsigned GetDaysofMonth(unsigned, unsigned, int);

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    yeah check your prototypes and

    monthindex is not initialized
    and when u return array[monthindex],,,,that causes an error

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    http://forums.devshed.com/c-programm...lt-332828.html
    Try to limit your questions to one forum or another - broadcasting questions like this only serves to show
    a) that you're in a hurry (in effect adding URGENT without saying it)
    b) ........ off people who answer on one forum only to see the same question on another.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 04:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM

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