-6

这里请参考代码中的注释。Segmentation Fault(core dumped) 发生在 *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;
}
4

1 回答 1

2

The pointer p is not initialized. It doesn't point to any storage.

Here you are trying to de-reference p and store the value from h:

*p = h;

But p doesn't point to any valid storage to hold that value.

于 2013-05-26T11:38:28.283 回答