本文介绍了指针和字符串分割造成的故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
char *str = "Hello";
printf("%c",++*str);
This gives segmentation fault on linux with gcc.The moment the first statement is changes to as
char str[10] = "Hello";
It works. What may be the reason?
解决方案
It is undefined behaviour to attempt to modify a string literal.
The compiler is free to place it in read-only memory (as it probably does in your case). Attempting to modify read-only memory is what's probably triggering the segfault.
这篇关于指针和字符串分割造成的故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!