Howdy, Stranger!

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

Categories

segmentation error

sfysfy Member Posts: 1
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

  • plotoploto Member Posts: 68
    : 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 .


  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    _______ // http://forcoder.org // free video tutorials and ebooks about [ C#, MATLAB, Swift, Visual Basic .NET, Scratch, C, Perl, Delphi, PL/SQL, Assembly, C++, PHP, Objective-C, Python, Go, R, Ruby, Visual Basic, JavaScript, Java Erlang, D, Bash, Logo, Julia, SAS, ML, Ada, ABAP, Transact-SQL, Scala, Rust, Lisp, FoxPro, Hack, LabVIEW, Apex, Alice, COBOL, Awk, Scheme, F#, Lua, Crystal, Clojure, VBScript, Fortran, Prolog, Dart, Kotlin ] ______________

Sign In or Register to comment.