C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-18-2008, 10:48 PM   #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;
}
kuljule is offline   Reply With Quote
Old 11-18-2008, 10:56 PM   #2
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
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.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim

Last edited by zacs7; 11-18-2008 at 10:59 PM.
zacs7 is offline   Reply With Quote
Old 11-18-2008, 11:02 PM   #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?
kuljule is offline   Reply With Quote
Old 11-18-2008, 11:07 PM   #4
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
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.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim

Last edited by zacs7; 11-18-2008 at 11:10 PM.
zacs7 is offline   Reply With Quote
Old 11-18-2008, 11:14 PM   #5
Registered User
 
Join Date: Nov 2008
Posts: 27
using gets(sent) doesn't change the segmentation error. But how do I initialize i?
kuljule is offline   Reply With Quote
Old 11-18-2008, 11:19 PM   #6
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
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.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 11-18-2008, 11:22 PM   #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("%d\n", leng);

what's the problem with this?
kuljule is offline   Reply With Quote
Old 11-18-2008, 11:25 PM   #8
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
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("%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.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim

Last edited by zacs7; 11-18-2008 at 11:30 PM.
zacs7 is offline   Reply With Quote
Old 11-18-2008, 11:33 PM   #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.
kuljule is offline   Reply With Quote
Old 11-18-2008, 11:48 PM   #10
Registered User
 
Join Date: Nov 2008
Posts: 27
nevermind figured it out. Thanks!
kuljule is offline   Reply With Quote
Old 11-18-2008, 11:52 PM   #11
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Reply

Tags
program, word count

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Avoiding Global variables csonx_p Windows Programming 32 05-19-2008 12:17 AM
brace-enclosed error jdc18 C++ Programming 53 05-03-2007 05:49 PM
First Time Using Linked Lists, Having Trouble Replacing First Node Peter5897 C Programming 7 06-16-2006 09:23 PM
load gif into program willc0de4food Windows Programming 14 01-11-2006 10:43 AM
Why wont my function exit correctly? LightsOut06 C Programming 2 10-09-2005 09:23 PM


All times are GMT -6. The time now is 09:37 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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