bool返回许多值

时间:2017-02-16 09:49:07

标签: c++ function return boolean

我试图从bool函数返回多个值,但是我收到“Segmentation fault(core dumped)”错误。我的代码是

#include<iostream>

using namespace std; 

bool te(int b,int *c,int *e){

    if (b>5){
        *c=68;
        return true;
    }
    else {
        *e=69;
        return false;
    }
}

int main() {
    int  y;
    int *z;
    int *r;

    cout<<"Give number:"<<endl;
    cin>>y;

    if(te(y,z,r)==1) {
        cout<<"b is >5"<<endl;
        cout<<*z<<endl;
    }
    else {
        cout<<"b is <5"<<endl;
        cout<<*r<<endl;
    }   

    return 0;
}

如果bool = false,它可以工作,但是当bool = true时我得到了分段错误。

1 个答案:

答案 0 :(得分:1)

您正在使用指针而不分配实际的物理内存。 你应该做点什么:

int y, z, r;

然后

if (te(y, &z, &r)) ...