Thread: Segmentation fault when changing a string

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    Question Segmentation fault when changing a string

    Code:
    #include<stdio.h>
    int main(){
      char *p = "Abc";
      *p = 'a';
      printf("%s\n", p);
      return 0;
    }
    It received SIGSEGV on the 3rd line. But if I use
    Code:
    char p[] = "Abc";
    , it goes well.
    So why I can't change *p when p is a pointer to a string? And, it's all right if I change it using gdb when I debug it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lilydjwg
    So why I can't change *p when p is a pointer to a string?
    Because it is not allowed to change the contents of a string literal, and attempts to do so result in undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Because it is not allowed to change the contents of a string literal, and attempts to do so result in undefined behaviour.
    If so, why can I when in gdb or use char p[] instead of char *p? What's the difference between them?

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Code:
    char *p = "abcde";
    char p[] = "abcde";
    In the first statement the address of the first element of the string literal "abcde" is assigned to p. This string literal is constant.

    The second case is an array assignment. Arrays can be changed. The second assignment is equal to:
    Code:
    char p[] = {'a', 'b', 'c', 'd', 'e', '\0'};
    Last edited by hilarius; 12-02-2009 at 04:24 AM. Reason: forgot the brackets - thanks, vart

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    char p
    is one char - not array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Quote Originally Posted by vart View Post
    Code:
    char p
    is one char - not array
    That's true: it should be:
    Code:
    char p[] = {'a', 'b', 'c', 'd', 'e', '\0'};
    Sorry for the inconvenience, but I adapted the original post.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    hilarius, so you mean two following two lines are the same, right?

    Code:
    char *p = "abc";
    const char *p = "abc";
    And, if you know C++, is it the reason that g++ will warn char *p = "abc" for p is not declared as const?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. segmentation fault??
    By snappleapple in forum C Programming
    Replies: 9
    Last Post: 04-27-2007, 11:56 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

Tags for this Thread