Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By DRK

Change one letter from a string

This is a discussion on Change one letter from a string within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { char* string="monkey"; char* ptr=string; *ptr='d'; puts(string); // Seg fault error } How to fix ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    24

    Change one letter from a string

    Code:
    #include <stdio.h>
    
    int main() {
       char* string="monkey";
       char* ptr=string;
       *ptr='d';
       puts(string); // Seg fault error
    }
    How to fix this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    By not doing this:
    Code:
    *ptr='d';


    Basically, you are not allowed to change a string literal. Any attempt to do so results in undefined behaviour.
    DRK likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    109
    Redefine string
    Code:
    char string[]="monkey";
    laserlight likes this.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you trying to make a donkey out of me?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
       char string[]={"monkey"};
       char* ptr=NULL;
       ptr= strchr(string, 'm');
       if(ptr)
           *ptr='d';
    
       puts(string); 
    
       return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by gunitinug View Post
    Code:
    #include <stdio.h>
    
    int main() {
       char* string="monkey";
       char* ptr=string;
       *ptr='d';
       puts(string); // Seg fault error
    }
    How to fix this?
    when you intialize a string constant to char pointer, the stringconstant is stored in a text or code (Which is read-only file).

    Code:
     char *string="monkey";
    So, redefine to a char array
    Code:
    char string[]="monkey";
    Understand the difference between string and string constant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-07-2011, 12:24 AM
  2. C random string with letter limitation
    By no4touchy in forum C Programming
    Replies: 6
    Last Post: 10-07-2009, 10:04 PM
  3. counting letter occurences in a string
    By pjr5043 in forum C++ Programming
    Replies: 35
    Last Post: 05-05-2008, 09:18 PM
  4. Remove a letter from a string
    By StarOrbs in forum C++ Programming
    Replies: 6
    Last Post: 04-05-2005, 03:03 PM
  5. checking if a string contains a letter
    By lonbgeach in forum C Programming
    Replies: 2
    Last Post: 04-07-2003, 03:40 PM

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