segmentation error

I wrote a piece of code which is the following.

#include

int main()
{

char *p="abc";
*p='m';
printf("%s",p);
return 0;

}

But I got segmentation error when I ran it. I know there must be something wrong in the first two lines of that function. But I can not
figure it out. how come it is not allowed to change one character of a string.

Anybody can explain it to me?
Thanks in advance

Comments

  • : I wrote a piece of code which is the following.
    :
    : #include
    :
    : int main()
    : {
    :
    : char *p="abc";
    : *p='m';
    : printf("%s",p);
    : return 0;
    :
    : }
    :
    : But I got segmentation error when I ran it. I know there must be something wrong in the first two lines of that function. But I can not
    : figure it out. how come it is not allowed to change one character of a string.
    :
    : Anybody can explain it to me?
    : Thanks in advance
    :
    :

    If you change the first line to :
    char p[]="abc";
    It will work.
    The reason u r not allowed to change characters with your code
    is u r pointing to constant string .


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion