Thread: Change string literal in C through pointer?

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    11

    Change string literal in C through pointer?

    is it possible to run this code?
    Code:
    #include <stdio.h>
    
    int main(void) {
        
      char *my_string="Hello"; 
      printf("%s",my_string);
      *my_string='w';
      printf("%c",my_string);
        
        return 0; 
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You are not allowed to modify a string literal. Doing so results in undefined behavior.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Generally, no. It may work on some systems, but it is definitely not portable.

    Technically, you should say:
    Code:
    const char *my_string = "Hello";
    If you need to be able to modify the string, then create an actual array to copy the string into:
    Code:
    char my_string[] = "Hello";
    Empty brackets case the compiler to make the char array exactly the correct size to hold the string, which is the string length + 1 for the null char that marks the end. If you need more space then you need to provide the size:
    Code:
    char my_string[100] = "Hello";

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    11
    Quote Originally Posted by Matticus View Post
    You are not allowed to modify a string literal. Doing so results in undefined behavior.
    why does it result in undefined behavior?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Because the standard mandates that this is the case.

    Suggested read: LLVM Project Blog: What Every C Programmer Should Know About Undefined Behavior #1/3

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The standard simply does not define the behavior of that operation ("undefined behavior"). Therefore it is completely non-portable. Any particular system may do something sane or insane. It's a crap shoot. Of course, if you're programming just for a particular system that allows it (perhaps an embedded one), and you don't care about portability in general, then it's perfectly okay to do it.

  7. #7
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    In the case of a string literal, the pointer to the string lives on the stack, and the string itself becomes a part of the executable which on many operating systems may end up in a portion of memory that is read only. In the case of the array, the entire string lives on the stack which in all systems will be writable memory.

  8. #8
    Registered User
    Join Date
    Dec 2016
    Posts
    11
    thanks for your replies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-14-2013, 08:21 PM
  2. Change one element of string pointed by char pointer
    By Ducky in forum C++ Programming
    Replies: 16
    Last Post: 02-15-2013, 09:29 PM
  3. Replies: 3
    Last Post: 01-15-2012, 06:09 AM
  4. Replies: 7
    Last Post: 07-18-2005, 08:43 AM
  5. String literal
    By subdene in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2002, 02:10 PM

Tags for this Thread