Segfaulting - don't know why

This is a discussion on Segfaulting - don't know why within the C Programming forums, part of the General Programming Boards category; The program's supposed to check if the entered string has embedded spaces (FYI, by the prof's definition, "Te xt" has ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    14

    Segfaulting - don't know why

    The program's supposed to check if the entered string has embedded spaces (FYI, by the prof's definition, "Te xt" has embedded spaces, while " text" and "text " do not, hence the extra bits of code). It gives me a segmentation fault sometime between the first and second "check" printfs in HasEmbedded. My test input is Cookie monster, FYI, but that shouldn't matter.

    If you're wondering, those "check" printfs are only tabbed so they stand out.

    Code:
       1 #include <stdio.h>
      2 int HasEmbedded(char str[]);
      3 int main() {
      4         char string[31];
      5         int space;
      6         printf("Enter a string: ");
      7         scanf("%30[^\n]", string);
      8 printf("String: %s\n", string);
      9         space = HasEmbedded(string);
     10         if (space > 0) printf("%s does have embedded spaces\n", string);
     11         else printf("%s does NOT have embedded spaces\n", string);
     12 }
     13
     14 int HasEmbedded(char str[]){
     15         int i = 0, k = 0, rv = 0;
     16
     17                                                         printf("check\n");
     18 //Ignore leading spaces
     19         while(str[i]=' '){
     20                 i++;
     21         }
     22
     23                                                         printf("check\n");
     24 /*
     25 If there is a space, enusure that it's follwed by a character
     26 (otherwise it's trailing, not embedded)
     27 Tehcnically, the curly braces could be omitted, but I prefer them for
     28 organizational purposes.
     29 */
     30         for(; str[i]; i++){
     31                                                         printf("check\n");
     32                 if(str[i] = ' '){
     33                                                         printf("check\n");
     34                         for(k=0; str[k]; k++){
     35                                                         printf("check\n");
     36                                 if(str[k]!= ' '){
     37                                                         printf("check\n");
     38                                         rv = 1;
     39                                                         printf("check\n");
     40                                 }
     41                         }
     42                 }
     43         }
     44                                                         printf("check\n");
     45         return(rv);
     46 }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    You're using = where you should be using ==

    In several places.
    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.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    while(str[i] = ' ')

    Your compiler should be screaming something at you but you are not paying any attention.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Argh, again with the single-character mistakes >.< Thanks guys.
    And no, my compiler didn't say anything.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    If your compiler doesn't say anything about that then either:

    a) your compiler is crap, or
    2) you are not setting your compiler to issue warnings at a high enough level

    Set your warnings as high as possible (how you do so is compiler-dependent) and fix them when you find them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segfaulting!
    By CMakesMeSad :( in forum C Programming
    Replies: 23
    Last Post: 07-10-2009, 01:04 PM
  2. why is strncpy segfaulting here?
    By Calef13 in forum C Programming
    Replies: 3
    Last Post: 12-29-2008, 02:27 PM
  3. Segfaulting Distance Program
    By radiohead in forum C Programming
    Replies: 2
    Last Post: 01-09-2006, 07:48 PM
  4. Why is it segfaulting?
    By XSquared in forum C Programming
    Replies: 7
    Last Post: 03-30-2004, 05:52 AM
  5. a segfaulting algorythm
    By demonus in forum C Programming
    Replies: 8
    Last Post: 08-11-2003, 08:06 AM

Tags for this Thread


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