因为我是c编程的初学者。我试图在程序中返回一个数组,但它不能正常工作。
这是密码

#include<stdio.h>
void main()
{
    int x,y,z;
    printf("Enter any three numbers separated with a single space : ");
    scanf("%d %d %d", &x, &y, &z);
    int * result = calc(x,y,z);
    printf("The big number is %d \n The middle number is %d \n The small number is %d", result[0], result[1], result[2]);
}
calc(int x, int y, int z){
    static int * result[3] = {0,0,0};
    if(x>y && x>z){
        result[0] = x;
        if(y<z){
            result[2] = y;
            result[1] = z;
        }
    }else if(y>x && y>z){
        result[0] = y;
        if(x<z){
            result[2] = x;
            result[1] = z;
        }
    }else if(z>x && z>y){
        result[0] = z;
        if(y<x){
            result[2] = y;
            result[1] = x;
        }
    }
    return result;
}

我搜索了很多,但要么我不明白,要么代码对我不起作用。

最佳答案

在calc函数中-

int *result[3] = {0,0,0};

是指向整数的三个指针的数组。
整数数组应该声明为
int result[3] = {0,0,0};

关于c - 返回c中的数组以计算数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54330961/

10-11 22:27
查看更多