Segmentation faults

This is a discussion on Segmentation faults within the C Programming forums, part of the General Programming Boards category; Hi all, Why the following code gives segmentation fault ???? Code: #include<stdio.h> int main() { char *p="hello"; printf("%s",p); printf("\n%c",*p); p++; ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Unhappy Segmentation faults

    Hi all,

    Why the following code gives segmentation fault ????

    Code:
    #include<stdio.h>
    
    int main()
    {
            char *p="hello";
            printf("%s",p);
            printf("\n%c",*p);
            p++;
            *p='g';
            printf("\n%s",p);
            return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are trying to modify a string literal. You shouldn't try to do that.
    Code:
    char p[] ="hello";
    You can modify six characters there. Five if you want it to remain a string.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    This thread goes a little deeper into why you can't do that: where the "String literals" store in ?

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    Quote Originally Posted by quzah View Post
    You are trying to modify a string literal. You shouldn't try to do that.
    Code:
    char p[] ="hello";
    You can modify six characters there. Five if you want it to remain a string.


    Quzah.
    This is also creating problems....
    Code:
    #include<stdio.h>
    
    int main()
    {
            char p[]="harpreet";
            printf("%s",p);
            printf("\n%c",*p);
            p++;
            *p='g';
            printf("\n%s",p);
            return 0;
    }
    The above code produces the following error...
    Code:
    Test.c: In function ‘main’:
    Test.c:8: error: lvalue required as increment operand
    What is the reason for this ???
    please help....

    Regards
    C_Enthuaist

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,817
    The reason is that you cannot increment an array. What you can do is to get a pointer to point to the first element of the array, and then increment that pointer.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation faults
    By movie55 in forum C Programming
    Replies: 8
    Last Post: 09-22-2009, 05:31 AM
  2. oldiofclose.c and segmentation faults
    By sd_padilla in forum C Programming
    Replies: 1
    Last Post: 12-11-2005, 01:24 PM
  3. Segmentation faults on Linked Lists. (Please help!!)
    By summerrainx in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2005, 06:23 AM
  4. segmentation faults pervade
    By ezwise in forum C Programming
    Replies: 8
    Last Post: 02-28-2005, 02:17 PM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM

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