Segmentation fault in c program runtime
来源:互联网 发布:吉祥网络 传奇 编辑:程序博客网 时间:2024/05/18 03:07
There is a common fault in c when using pointer to represent array or string. For example
#include <stdio.h>
void main(){
char *p="china";
printf("%s",p);
}
This program will get the result
china
However, when you want to add a line to change the element of the string, like
#include <stdio.h>
void main(){
char *p="china";
p[1]='x'; //line 3
printf("%s",p);
}
The program will not have problem to compile. But when running it on gcc or debug it, you will see a segmentation fault when the program goes to line 3.
I have looked for the reason of this fault for a while. Mostly they will talk about I want to write to the memory which is read only. But no one tell me how to handle this problem.
Yes, I know if I first declare a array and assay the address to the pointer, but the question is If I can "assign" a string to the pointer, why I cannot change the value in the pointer?
Hopefully I can find the solution.
- Segmentation fault in c program runtime
- error: Program received signal SIGSEGV, Segmentation fault. (Codeblocks, C++)
- error: Program received signal SIGSEGV, Segmentation fault. (Codeblocks, C++)(2)
- Segmentation fault in Linux
- Segmentation fault in Linux
- Program received signal SIGSEGV, Segmentation fault.
- Program received signal SIGSEGV, Segmentation fault.
- Program received signal SIGSEGV, Segmentation fault.
- Program received signal SIGSEGV, Segmentation fault
- Program terminated with signal 11, Segmentation fault.
- program received signal SIGSEGV, Segmentation fault
- [转载]Segmentation fault in Linux
- Linux C程序:Segmentation fault
- segmentation fault(linux c)补遗
- About Segmentation Fault in Linux ( SIGSEGV )
- About Segmentation Fault in Linux ( SIGSEGV )
- About Segmentation Fault in Linux ( SIGSEGV )
- 阅读segmentation fault in Linux.pdf
- vim自动补全代码(代码提示)以及我的vim配置文件
- 在自己写代码获得东西
- 你必须十分努力,才能看起来毫不费力
- QOS技术
- C#如何写类库,并调用类库中的函数
- Segmentation fault in c program runtime
- dynamic_cast 全解说
- 随想录(管理自己)
- 使用java.lang.Math 生成随机数字
- dynamic_cast
- 独立开发者如何笑傲应用商店?
- static_const 和const_cast
- 面试时问你的一些大数据存储问题
- sendredirect()和forward()的区别