Thread: Very odd segmentation fault/core dump.

  1. #16
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    That's working great. Now all I have to do is write functions to determine the number of vowels, characters, and words, as well as one to test if it's a palindrome or not. Then, I'll be all set =D

  2. #17
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    One last question. Should the following code work to determine the number of words in a string? I.e. I'm assuming all words are seperated by spaces.

    Code:
    for( i = 0; i < strLen; i++ )
       {
          if ( isspace (strArray[input-1][i] == 0 )
          {
             numWords++;
          }
       }
    The man page says isspace should return a zero for true. Thus, every time the function returns a 0, it indicates another word. Or, for a single-word string ( no spaces ), I simply say numWords = 1. Whenever I run it like this, it simply counts the number of letters, not white spaces. For the string "anna" it tells me there are 4 words. Any ideas?

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > char buffer[101];
    > char buf[100];
    Why the change in size?
    Carefully crafted input files would result in more calls to fgets() in the 2nd loop than in the first loop, thus throwing off your count.

    > while ( fgets ( buf, 100, data ) != NULL )
    Use
    while ( fgets ( buf, sizeof buf, data ) != NULL )

    > rewind ( data );
    This should really be in ReadNumStrings()

    Similarly, main() should be in charge of calling functions
    Code:
    FillArray ( data, strArray, numStrings );
    userInput = MenuOne ( numStrings, strArray );
    MenuTwo ( userInput, strArray );
    It keeps your functions focussed on one specific task

    > strArray[counter] = (char *) malloc (strLen * sizeof (char));
    There is no need to cast malloc - this is C
    Also, you're short by a char - you forgot to count the \0
    strArray[counter] = malloc ( (strLen+1) * sizeof (char));

    One more thing, next time you have so much source code, use ZIP and use the attach file feature. Makes it much easier for us to simply download and unzip
    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.

  4. #19
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The man page says isspace should return a zero for true.
    it should say isspace returns true (nonzero) for a space and 0 for a non-space.

  5. #20
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    Quote Originally Posted by nonpuz
    it should say isspace returns true (nonzero) for a space and 0 for a non-space.
    You're absolutely right, I read it wrong

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What am I doing worng? Segmentation fault, core dump
    By cookie in forum C Programming
    Replies: 4
    Last Post: 06-08-2007, 09:59 AM
  2. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  3. Core Dump / Segmentation Fault
    By Inquirer in forum Linux Programming
    Replies: 2
    Last Post: 04-08-2003, 08:24 PM
  4. odd segmentation fault occurence
    By gazsux in forum C Programming
    Replies: 6
    Last Post: 12-13-2002, 09:02 AM
  5. segmentation core dump - need help
    By knight101 in forum C++ Programming
    Replies: 1
    Last Post: 11-26-2001, 04:43 PM