简体   繁体   中英

Unhandled exception at C++

I receive this error

Unhandled exception at 0x00091e11 in JobTest.exe: 0xC0000005: Access violation writing location 0x0009573c.

on first line of this function

void myFunction(char str[]) {

    str[0] = 'C';// here is a problem
    printf(str);
}

myFunction("Hello World");

in visual studio 2010. Is it compiler specific or i am doing really bad job. i also tried by changing function signature char *str.

String literals are non-modifiable. You are trying to modify a string literal in function myFunction .
String literals might be shared and could be stored in read-only memory (as @Duplicator said in his comment). Any attempt to modify a string literal invokes undefined behavior .

As @MooseBoys suggested, You can fix it by changing it to:

char msg[] = "Hello World"; 
myFunction(msg);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM