Thread: Segmentation fault when replacing a char in an array

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    Segmentation fault when replacing a char in an array

    This function should replace all instances of a character in a given character array, while returning the amount of characters changed, but I keep getting a segmentation fault at the highlighted area.

    I'm only supposed to use pointers so arrays are out of the question, and I don't think we are allowed to use the string.h library as well.

    Any ideas on how I could avoid something the segmentation fault or ways to fix it?

    Code:
    int replaceChars(char replace, char find, char *input) {   int i, j;
       //Finds length
       for(i = 0; *(input + (i + 1)) != '\0'; i++)
       ;
       int c = 0;
       for(j = 0; j <= i; j++) {
          if(*(input + j) == find) {
             *(input + j) = replace; //Segmentation fault happens here
             c++;
          }
       }
       return c;
    }
    Last edited by seemethere; 01-25-2013 at 06:35 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Best guess: you're passing the function a char *str ="something", instead of char str[] ="something"?

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I would guess your find length does not work on an empty string.
    I could see this causing in to crash.

    I suggest making your own copy of strlen and testing that so it works right; then doing this function again.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Quote Originally Posted by Tclausex View Post
    Best guess: you're passing the function a char *str ="something", instead of char str[] ="something"?

    This actually worked thank you!

    Any idea why it's like that?
    Last edited by seemethere; 01-25-2013 at 06:54 PM.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by seemethere View Post
    This actually worked thank you!

    Any idea why it's like that?
    Because char * s = "string"; is a string literal and can NOT be modified. When using them you should really define them as:
    Code:
    const char * s = "some string literal";
    or the even more pedantic:
    Code:
    const char * const s = "some string literal";
    A character array _initialized_ with a string literal, IS modifiable. ie:
    Code:
    char s[] = "some string literal";
    but still note that it can't hold more then the length of the string it was initialized with (or whatever is specified inside the [] if any).

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Here is a simple program showing what happens when you define the variable as such and then try to do something like you attempted:
    Code:
    $ cat t.c 
    int main(void)
    {
        const char * const s = "help me";
        /* try to assign s to a different address */
        s = "some new string";
        /* try to modify what s is pointing to */
        *s = 'k';
        return 0;
    }
    $ gcc t.c
    t.c: In function ‘main’:
    t.c:5:5: error: assignment of read-only variable ‘s’
    t.c:7:5: error: assignment of read-only location ‘*s’
    It wont even compile now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault char**
    By kunalnandi in forum C Programming
    Replies: 4
    Last Post: 03-13-2012, 09:37 AM
  2. segmentation fault when using char*[] (please help)
    By mickmos in forum C Programming
    Replies: 12
    Last Post: 10-26-2011, 05:38 AM
  3. Segmentation fault when appending to strings (char *)
    By LanguidLegend in forum C Programming
    Replies: 15
    Last Post: 02-24-2011, 07:35 PM
  4. Replies: 8
    Last Post: 12-08-2009, 02:47 AM
  5. Segmentation fault with structs and char pointers
    By Keybone in forum C++ Programming
    Replies: 20
    Last Post: 01-17-2004, 01:36 PM