小编Shi*_*nai的帖子

使用指针在C中的一个函数中返回多个值

我在互联网上搜索了我的问题但它不会得到解决.我有以下功能:

#include <stdio.h>
#include <math.h>

int solve(double a, double b, double c, double *x1, double *x2){
    *x1=(-b+sqrt(pow(b,2)-(4*a*c)))/(2*a);
    *x2=(-b-sqrt(pow(b,2)-(4*a*c)))/(2*a);
}

int main(void){
    double a, b, c;
    scanf("%lf", &a);
    scanf("%lf", &b);
    scanf("%lf", &c);
    double x1, x2;
    int count= solve(a, b, c, &x1, &x2);
    if(!count){
        printf("no solution");}
    else if(count==1){
        printf("one solution x: %lf", x1);}
    else if(count>1){
        printf("two solutions x1: %lf x2: %lf", x1, x2);}
}
Run Code Online (Sandbox Code Playgroud)

程序应该从函数solve中返回两个值,但每次启动程序时我都会收到一条警告"缺少返回值".我的错在哪里?顺便说一句,struct不是我教授限制的选项,它需要在一个函数中完成.

c return-value

1
推荐指数
1
解决办法
1172
查看次数

标签 统计

c ×1

return-value ×1