本文介绍了段错误而deferencing字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#包括LT&;&stdio.h中GT;
INT主要(无效)
{
的char * p =字符串是好的;
的printf(%S,* P);
返回0;
}
可能有人请告诉我为什么我在这里得到段错误?
解决方案
C11: 7.21.6 Formatted input/output functions:
3.4.3
*p
is of type char
. %s
expects argument of type char *
. This will invoke undefined behavior. In this case anything could happen. Sometimes you may get the expected result and sometimes not. This may also cause program crash or segmentation fault (which is the case here).
For %s
, you need to pass the starting address of the sting/literal.
Change
printf("%s",*p);
to
printf("%s",p);
这篇关于段错误而deferencing字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!