Thread: word count program - Please HELP!!! Segmentation error!!!

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    27

    Unhappy word count program - Please HELP!!! Segmentation error!!!

    I keep getting a segmentation error and i don't know why. Can you please help?

    Thank you!

    Here's the program:


    insert
    Code:
    /*This program is suppose to take a sentence as input (in a string) and compute the number of words in the sentence.
    */
    
    
    
    #include <stdio.h>
    #include <string.h>
    
    int main()
    
    {
            int i, leng, number_words;
            char sentence[i];
            char *sent = sentence;
            char x;
    
    //      while (scanf("%s", sent) == 1)
    //              printf("%s\n", sent);
    
            scanf("%[^\n]", sent);
            printf("%s\n", sent);
    
            leng = strlen(sentence);
            printf("%d\n", leng);
            
            number_words=1;
            for(i=0; i<leng); i++)
            {
                    if ((sentence[i] == ' ') || (sentence[i] == ','))
                            number_words = number_words + 1;
            }
             
            printf("the number of words is %d\n", number_words);
            return 0;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    int i, leng, number_words;
            char sentence[i];
    I assume this is C99. So it will be created with a random length since i is uninitialized.

    Thus when you write to it, segfault because you probably go past the end -- who knows!

    Oh and don't use scanf for strings. Use fgets() -- see the FAQ.
    Last edited by zacs7; 11-18-2008 at 10:59 PM.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    What do you mean i is not initialized? Isn't it initialized in the for loop?
    Also, I here bad talk about using fgets. Why do people go against using it? What's the difference between fgets and scanf of a string?

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No, you use i when you declare 'something'.

    http://cboard.cprogramming.com/showp...37&postcount=9

    There is nothing wrong with using fgets(), people against using it probably have no idea why they're against it. The only bad thing is it reads in the newline (that's not bad), you can remove the newline -- see the FAQ.
    Last edited by zacs7; 11-18-2008 at 11:10 PM.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    using gets(sent) doesn't change the segmentation error. But how do I initialize i?

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't use gets() -- see the FAQ.

    > But how do I initialize i?
    Oh I don't know,
    Code:
    int i = 50;
    maybe?

    But that's only half the problem. Perhaps read the first few chapters of a good C programming book... or the tutorials on this site.

    Is this for CSCI 261 class (Computer Science 1)? If so, some of your "buddies" have asked the same question in the last few days.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    Its C Programming. That's the class. The thing about initializing it means that it won't work if its greater than that number. I want to be able to do this with any length of a string. I think the segmentation error is coming from

    leng = strlen(sentence);
    printf("&#37;d\n", leng);

    what's the problem with this?

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Nothing. If you're not going to listen, I won't bother.

    I've told you where the segfault is coming from, FIX IT.

    Ignoring the pointless pointer,

    Consider this,
    Code:
    #define USELESS 5
    
    char sentence[USELESS];
    
    scanf("&#37;s", sentence);
    Now everytime your program runs, imagine USELESS changes, it could be 5, 4, -1020, 532982953. It doesn't matter. Which means you've only got enough room to store a string that big (minus the NUL character). And you'll probably write past the end -- hence the segfault. In other words 'i' can have any value an int can have.

    Now consider:
    Code:
    char sentence[256];   /* magic number, perhaps define this somewhere. Or use BUFSIZ (look it up) */
    
    /* TODO: check the return result of fgets() */
    fgets(sentence, sizeof(sentence), stdin);
    If you need "any size string" then that's a bit more advanced.
    Last edited by zacs7; 11-18-2008 at 11:30 PM.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    I'm sorry if I am seeming like I'm not listening. I am, but I am just really confused. Can you repeat where the seg fault is coming from? The program prints out the sentence but then after printing it out, it says seg fault.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    nevermind figured it out. Thanks!

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Say your random-length sentence was 5 chars, and then you read in 20 chars.

    Where do the other 15 go - answer, over someone elses memory.

    When do you get the segfault, when that someone else comes back to find the values they thought were safe have in fact been trashed by your sloppy code.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM

Tags for this Thread