【发布时间】:2021-04-28 04:35:01
【问题描述】:
在要求计算输出的测试中找到以下代码。
#include <stdio.h>
int gate(char *P)
{
char *q = P;
q++;
*q++;
++*q;
return(q-P);
}
int main()
{
char *s = "gateexam";
int x = gate(s);
printf("%d",x);
}
在在线编译器上运行它,但由于“++*q”行(注释掉该行使程序运行良好),由于某种原因导致分段错误。
不明白是什么原因造成的
【问题讨论】:
标签: pointers segmentation-fault dereference pre-increment