Like Tree1Likes
  • 1 Post By rags_to_riches

Segmentation Fault?

This is a discussion on Segmentation Fault? within the C Programming forums, part of the General Programming Boards category; Hello! I just completed writing a program to ask a user for 2 words, pass them into a function, and ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    Segmentation Fault?

    Hello!

    I just completed writing a program to ask a user for 2 words, pass them into a function, and use strcmp to let the user know which word is larger (or if they are equal). The program compiled and runs, but at the end it tells me 'Segmentation fault (core dumped)'. What does this mean? The only thing I could think of causing it is the 'char *msg = NULL' and printf using it, as this was something the professor told us to include, but never once in the class or the notes explains why? That's the only thing I can think of. Anyway, here is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void word_order(char *string1, char *string2);  /* Create prototype of new function */
    
    int main()
    {
    
            char string1[30];
            char string2[30];
            /* create rest of code here */
    
            printf("\nPlease enter a word no more than 30 characters: ");
            scanf("%s", string1);
            printf("\nPlease enter another word no more than 30 characters: ");
            scanf("%s", string2);
            word_order(string1, string2);
    
            return 0;
    }
    
    void word_order(char *string1, char *string2)  /* New function here. */
    {
            char *msg = NULL;
    
            /* create rest of code here */
            if (strcmp(string1, string2) > 0)
            { printf("First word is bigger than the second\n");}
            else if (strcmp(string1, string2) < 0)
            { printf("Second word is bigger than the first\n");}
            else
            {printf("Word one is equal to word two\n");}
            printf("%s\n", msg);
    }
    Any advice would be great! Thank you!

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    Code:
    char *msg = NULL;
    /* Other code that does nothing with msg */
    printf("%s\n", msg);
    So you're printing NULL, which is undefined behavior. In this case, your compiler's behavior (actually, maybe it's the C runtime's decision) is to segfault. Some compilers might print "(null)" when you do this. That's the problem with engaging in programming that invokes undefined behavior.
    Salem likes this.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    1
    I believe a "segmentation fault" is when you try to assign a variable that tries to access memory that is unavailable to you.

    From glancing through your code, it seems you are missing an & (ampersand) in lines 14 and 16 before your strings

    Original:

    Code:
    printf("\nPlease enter a word no more than 30 characters: "); scanf("%s", string1); printf("\nPlease enter another word no more than 30 characters: "); scanf("%s", string2);


    Modified:

    Code:
    printf("\nPlease enter a word no more than 30 characters: "); scanf("%s", &string1); printf("\nPlease enter another word no more than 30 characters: "); scanf("%s", &string2);
    Last edited by Kentric; 11-17-2011 at 12:36 PM.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Kentric:

    I didn't put an '&' because I was taught that you do not need one when you are working with arrays (both & and arrays point to a memory location).

    rags_to_riches:

    Thanks for your help! So.. I am still confused. Is that what I want to be happening based on what he wanted me to enter? Or is it something I failed to do? I have run the program just fine by removing it (the 'char *msg = NULL'), and I don't get the segmentation fault. Why did he want us to include it? This is something that was never covered, so I have no idea why he wanted us to include it in our code.

    Thank you so much again!

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Using & when you have a char array is unnecessary, but largely harmless (though gcc will complain sometimes with sufficient warnings enabled).

    Using & when you have a char pointer is flat out wrong.

    Saying scanf("%s", string); is just the same as scanf("%s", &string[0]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Yes, string1 is equivalent to &string1[0]. Array names are converted to 'pointer to type' when used in an expression, except for the sizeof and unary & operators.

    As for NULL, it is a void pointer guaranteed to not point to a real memory address. I'm not sure what your instructor is expecting you to use it for in this context. I suspect he/she wants some code that conditionally assigns msg to something other than NULL. If you are to use value of msg to output something, you would have to use it with an IF,
    Code:
    if( NULL == msg ) {...}
    Clearly, as you know now, you can't use it to actually reference something if it's still pointing to NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Segmentation Fault Help
    By cow_tipper8282 in forum C Programming
    Replies: 5
    Last Post: 10-16-2010, 03:25 PM
  2. Segmentation Fault
    By daniel-lemke in forum C Programming
    Replies: 3
    Last Post: 10-15-2010, 12:03 PM
  3. Segmentation Fault hmm?
    By pobri19 in forum C Programming
    Replies: 4
    Last Post: 05-03-2008, 07:51 AM
  4. segmentation fault when using gcc
    By stodd04 in forum C Programming
    Replies: 6
    Last Post: 02-14-2005, 06:34 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

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