Segmentation fault when changing a string

This is a discussion on Segmentation fault when changing a string within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main(){ char *p = "Abc"; *p = 'a'; printf("%s\n", p); return 0; } It received SIGSEGV on ...

  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
    22,159
    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.
    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
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    char p
    is one char - not array
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 10:11 AM
  2. segmentation fault??
    By snappleapple in forum C Programming
    Replies: 9
    Last Post: 04-28-2007, 12:56 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10: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, 04:58 PM

Tags for this Thread


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