Thread: segmentation fault

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    segmentation fault

    heyas,

    the replace fuction below is causing a segmentation fault -not sure why. this is really frustrating.. any help would be much appreciative.

    the line causing the problem is:
    Code:
    (*newln) = new;
    the whole code is below:
    Code:
    #include <stdio.h>
    
    char *replace(char *text, char old, char new); 
    
    int main(int argc, char *argv[])
    {
    
            printf("%s", replace("Old sontence", 'o', 'e'));
            return 0;
    
    }
    
    char * replace(char *text, char old, char new)
    {
    
            char *newln; 
           	 newln = text;
    		while(*newln)
    		{
                   		 if (*newln == old)
                      			(*newln) = new;
                		newln++;
           		 }
            return text; 
    }
    thanks in advance

    -twans

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > printf("%s", replace("Old sontence", 'o', 'e'));
    String constants can't be modified

    char msg[] = "Old sontence";
    printf("%s", replace(msg, 'o', 'e'));
    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.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    salem, again you have a quick and accurate response.

    thanks!

    :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM