This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center。
6年前关闭。
这里请参考代码中的注释。段错误(核心已转储)在* p = h行中发生。但是当我在另一个新的c文件中分别运行此行时,这是完全可以的
6年前关闭。
这里请参考代码中的注释。段错误(核心已转储)在* p = h行中发生。但是当我在另一个新的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
未初始化。它不指向任何存储。
在这里,您尝试取消引用p并存储h
中的值:*p = h;
但是p
不会指向任何有效的存储来保存该值。