如何修改只读内存中的char

时间:2022-09-11 17:57:55

When I try to edit the value of a char pointer like this, I get an access violation. I know, that the compiler locates this in a read-only memory block, but is there any way to unlock this likeGlobalUnlock() or HeapUnlock()

当我尝试编辑像这样的char指针的值时,我得到访问冲突。我知道,编译器将它定位在只读内存块中,但有没有办法解锁这个像globalUnlock()或HeapUnlock()

int main()
{
    char* foo = "Hello";
    *foo = 'B'
}

1 个解决方案

#1


5  

There is no need to use lock/unlock. Keep it simple. If you intend to modify the string, use a char array or a std::string.

无需使用锁定/解锁。把事情简单化。如果您打算修改字符串,请使用char数组或std :: string。

char foo[] = "Hello";
*foo = 'B'

or

std::string foo = "Hello";
foo[0] = 'B'

#1


5  

There is no need to use lock/unlock. Keep it simple. If you intend to modify the string, use a char array or a std::string.

无需使用锁定/解锁。把事情简单化。如果您打算修改字符串,请使用char数组或std :: string。

char foo[] = "Hello";
*foo = 'B'

or

std::string foo = "Hello";
foo[0] = 'B'