Thread: Function month_day: Segmentation fault

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    6

    Question Function month_day: Segmentation fault

    In theory, the function should work:
    Code:
    #include <stdio.h>
    
    static char daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                                    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} };
    
    int day_of_year(int year, int month, int day)
    {
        int i, leap;
        leap = year%4 == 0 && year%100 != 0 && year%400 == 0;
        for (i = 1; i < month; i++) day += daytab[leap][i];
        return day;
    }
    
    void month_day(int year, int yearday, int *pmonth, int *pday)
    {
        int i, leap;
        leap = year%4 == 0 && year%100 != 0 || year%400 == 0;
        for (i = 1; yearday > daytab[leap][i]; i++) yearday -= daytab[leap][i];
        *pmonth = i;
        *pday = yearday;
    }
    
    int main()
    {
        printf("%d\n", day_of_year(2011, 2, 29));
        int day=60, year=2012;
        int *pm, *pd;
        month_day(year, day, pm, pd);
        printf("%d:%d\n", pm, pd);
    }
    But it produces a segmentation error

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I can immediately see the problem. It's actually in main, not month_day.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Where have you allocated memory for those pointers you're using in main()?

    Why are you even using pointers for these variables instead of just using normal non-pointer variables for those values and then passing the address of these variables in the function call?

    By the way a decent properly configured compiler should be able to warn you about most of the issues with your program.

    Perhaps you meant something more like:
    Code:
        int pm, pd;
        month_day(year, day, &pm, &pd);
        printf("%d:%d\n", pm, pd);
    Jim

  4. #4
    Registered User
    Join Date
    Jul 2017
    Posts
    6
    Quote Originally Posted by jimblumberg View Post
    Where have you allocated memory for those pointers you're using in main()?

    Why are you even using pointers for these variables instead of just using normal non-pointer variables for those values and then passing the address of these variables in the function call?

    By the way a decent properly configured compiler should be able to warn you about most of the issues with your program.

    Perhaps you meant something more like:
    Code:
        int pm, pd;
        month_day(year, day, &pm, &pd);
        printf("%d:%d\n", pm, pd);
    Jim
    I did not ask to ask me questions, I took this example from the textbook. I read a bad textbook and therefore I need answers and not questions. All problem solved

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I did not ask to ask me questions
    Why not? If you never have to think about what may be causing the problems then you will never learn to solve these kind of simple problems.

    I took this example from the textbook.
    So why didn't you state this fact in your first post. I'm not clairvoyant so how should I know anything about the problem other than what you posted.

    I read a bad textbook and therefore I need answers and not questions.
    So maybe the real answer is for you to talk to your instructor about the "failings" of your textbook. Since you're a beginner what makes you think that the textbook is so bad? Perhaps you misunderstood the point of the code you posted? Perhaps that code is intended bad code designed to illustrate some point made in the text of the book. Without knowing anything about the book I would take your "bad textbook" complaint with some skepticism.

    Jim

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Super-Man View Post
    I did not ask to ask me questions, I took this example from the textbook. I read a bad textbook and therefore I need answers and not questions. All problem solved
    You are (still) retarded.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault in ioctl function
    By Venturina in forum C Programming
    Replies: 1
    Last Post: 08-19-2014, 11:24 AM
  2. segmentation fault from sprintf function
    By stdio in forum C Programming
    Replies: 2
    Last Post: 03-11-2012, 03:30 AM
  3. Segmentation fault on recursive function
    By xbfish in forum C Programming
    Replies: 12
    Last Post: 10-12-2011, 10:44 AM
  4. Recursive function and segmentation fault
    By davidjg in forum C Programming
    Replies: 4
    Last Post: 02-11-2011, 10:23 AM
  5. Segmentation fault: vector to function
    By ArlexBee-871RBO in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2008, 04:36 AM

Tags for this Thread