修改C字符串:访问冲突 [英] Modifying a C string: access violation

查看:85
本文介绍了修改C字符串:访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能显示的文件:

  <一href=\"http://stackoverflow.com/questions/164194/why-does-simple-c-$c$c-receive-segmentation-fault\">Why没有简单的C code收到分段错误?

  修改C字符串常量?


为什么这个code生成访问冲突?

  INT的main()
{
字符* MyString的=5;
* MyString的='E'; //崩溃
返回0;
}


解决方案

* MyString的是在只读静态存储器显然​​指向。 C编译器可在只读存储器,这可能不是在运行时被写入分配字符串。

Possible Duplicates:
Why does simple C code receive segmentation fault?
Modifying C string constants?

Why does this code generate an access violation?

int main()
{
	char* myString = "5";
	*myString = 'e'; // Crash
	return 0;
}

解决方案

*mystring is apparently pointing at read-only static memory. C compilers may allocate string literals in read-only storage, which may not be written to at run time.

这篇关于修改C字符串:访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆