这里请参考代码中的注释。分段错误(核心转储)发生在这里的行* p = h。但是,当我在分开的另一个新的C文件单独运行这条线是完全没问题C(核心转储)中的分段错误 - 简单的指针
#include<stdio.h>
int *max(int *a,int *b)
{
if(*a>*b)
{
return a;
}
else
{
return b;
}
}
int main()
{
int h=1;
int *p;
int i=1,j=2,k=3;
int *a,*b,*c,*d;
c=max(&i,&j);
d=&i;
printf("\nOutput from the max function %d\n",*c);
printf("\n%d\n",*d);
*p=h; // Line where segmentation fault is occurring
printf("\n%d\n",*p);
return 0;
}
'p'未初始化。 – BLUEPIXY
它应该是'p =&h;'和'p = malloc(sizeof(int))之一; * p = h;' – A4L
在这里你不需要使用带'p'的指针。只需将其声明为'int p;'并赋予'p = h'即可。 –