Very odd segmentation fault/core dump.

This is a discussion on Very odd segmentation fault/core dump. within the C Programming forums, part of the General Programming Boards category; That's working great. Now all I have to do is write functions to determine the number of vowels, characters, and ...

  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
    32,824
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #19
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    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

Page 2 of 2 FirstFirst 12
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, 10: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, 09: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

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