Thread: Problem with segmentation fault

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    4

    Problem with segmentation fault

    Hi everyone,

    I have been having trouble writing a seemingly easy program. My program must remove all white space from a string without the use of string functions or an auxiliary string.

    Here is my attempt to solve this,

    Code:
    void removewhitespace(char *str)
    {
      int i=0,j=0;
      while(str[i] != '\0'){
        if(str[i] != (' '||'\n'||'\t')){ //both characters increment
          str[j]=str[i];
          i++;
          j++;}
        else{ //if character is white space, i increments and starts replacing the white space at j.
          i++;
        }
      }
    }
    I get a segmentation fault on line 6. I honestly can't figure out what is causing this problem. I would think that my while statement would stop the program from accessing memory beyond the bounds of the string.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    if (str[i] != (' '||'\n'||'\t'))
    This does not do what you think.

    It should be more like
    Code:
    if ((str[i] != ' ') && (str[i] != '\n') && (str[i] != '\t'))
    Fact - Beethoven wrote his first symphony in C

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, check that the input "str" is being passed into the function ok.

    Use a printf statement.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Thanks for the help with that if statement. I still can't figure out why I am getting a segmentation fault though. I am debugging the program in gdb and str[i] and str[j] both print out as the first letter of my input string after I run the program once.
    In case it helps, this is the program I am using to test my function,

    Code:
    void main(int argc, char **argv){
    char *teststring = "I'm doing  great!\t\n";
    removewhitespace(teststring);
    printf("%s\n", teststring);
    }

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Try this:
    Code:
    char teststring[] = "I'm doing  great!\t\n";
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Thank you so much, that fixed my segmentation fault problem. Why is it that the string created by a pointer doesn't work the same as a string created as an array?

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I found in the C99 draft

    6.7.8 Initialization #32

    EXAMPLE 8 The declaration
    Code:
            char s[] = "abc", t[3] = "abc";
    defines ``plain'' char array objects s and t whose elements are initialized with character string literals. This declaration is identical to
    Code:
            char s[] = { 'a', 'b', 'c', '\0' },
                 t[] = { 'a', 'b', 'c' };
    The contents of the arrays are modifiable. On the other hand, the declaration

    Code:
    char *p = "abc";


    defines p with type ``pointer to char'' and initializes it to point to an object with type ``array of char'' with length 4 whose elements are initialized with a character string literal. If an attempt is made to use p to modify the contents of the array, the behavior is undefined.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By rhayne in forum C Programming
    Replies: 2
    Last Post: 04-07-2012, 11:58 PM
  2. Segmentation fault problem
    By raczzoli in forum C Programming
    Replies: 4
    Last Post: 07-18-2011, 06:49 PM
  3. MPI Problem gives segmentation fault
    By confused_coder in forum C Programming
    Replies: 6
    Last Post: 09-07-2009, 09:34 PM
  4. problem with segmentation fault
    By cBegginer in forum C Programming
    Replies: 13
    Last Post: 05-15-2005, 11:51 AM
  5. Segmentation fault problem
    By robsmith in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 05:33 PM