Thread: Segmentation Fault

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    Redding, CA
    Posts
    2

    Segmentation Fault

    I have a program that asks for the day, month, and year and returns a formatted version. For example, if I entered what the program currently checks for (28 for the day, 7 for the month, and 2010 for the year), it will print July 28, 2010. However, I am getting a segmentation fault which gdb is telling me is on line 25, which I have highlighted. Can anyone help with this issue?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /* Prototypes */
    void Get_New_Day(int *x, char *y);
    void Get_New_Month(int *x, char *y);
    
    int main() {
        int day;
        int month;
        int year;
        char new_day[100];
        char new_month[100];
        char new_string[100];
    
        printf("\nEnter the day: ");
        scanf("%d", &day);
    
        printf("\nEnter the month: ");
        scanf("%d", &month);
    
        printf("\nEnter the year: ");
        scanf("%d", &year);
    
        Get_New_Day(&day, new_day);
        Get_New_Month(&month, new_month);
    
        printf("\nIt is %s %s, %d.\n\n", new_month, new_day, year);
    
        return 0;
    }
    
    void Get_New_Day(int *x, char *y) {
        switch (*x) {
            case 28:
                strcpy(*y, "28th");
                break;
    
            default:
                strcpy(*y, "null");
                break;
        }
    }
    
    void Get_New_Month(int *x, char *y) {
        switch (*x) {
            case 7:
                strcpy(*y, "March");
                break;
    
            default:
                strcpy(*y, "NULL");
                break;
        }
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You want to strcpy in y, not *y.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    Redding, CA
    Posts
    2
    How my goodness, it was so simple. Thank you claudiu, it works just fine now.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by daniel-lemke View Post
    How my goodness, it was so simple. Thank you claudiu, it works just fine now.
    You are welcome. When you were dereferencing y you were basically trying to copy into y[0] which is just one char, hence the segfault.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd Segmentation Fault
    By JohnnyAppleseed in forum C Programming
    Replies: 7
    Last Post: 08-24-2010, 12:01 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