线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)错误

时间:2018-03-07 21:36:37

标签: c++ xcode pointers

有人可以帮助解释为什么代码会构建但会中断并发出错误线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)?

void biggest(int value1, int value2, int * resultPtr)
{
    if( value1 > value2 )
    {
        *resultPtr = value1;
    }
    else
    {
        *resultPtr = value2;     //the code breaks on this line
    }
}

int main()
{
    int* p;
    biggest(15, 20, p);
    cout << "The biggest value is " << *p << endl;
    return( 0 );
}

1 个答案:

答案 0 :(得分:4)

p为null,您尝试在最大内部写入。

你可能想要:

int main()
{
    int p;
    biggest(15, 20, &p);
    cout << "The biggest value is " << p << endl;
    return( 0 );
}