2017-04-12 70 views
2

我正在尝试使用函数来计算h的值,然后将这些值输入到将计算n的方程中。这是我的代码目前看起来像......返回具有多个值的两个变量

int findN(double xI, double xF) { 


double h = 0.1; 
int n; 

do { 
    printf_s("%8.5f \n", h); 
    n = ((xF - xI)/h); 
    h = h/10; 


    printf_s("%6d \n", n); 
} while (h >= 0.00001); 


return n; 
} 

我知道,这个功能将只返回否当前,但由于我是新来这个我不确定要怎么也回h的所有值以及n的所有值...如果有人可以帮助我,并告诉我如何返回n的所有值,它将不胜感激。

感谢。

+2

创建一个地方来存储它们并返回一个指针。 – jthill

+1

你想返回h和n的最终值,还是返回所有这些值的数组/列表? – MateoConLechuga

+1

在C中搜索数组,并将数组传递给函数。 – Evert

回答

0

如果您想了解更多信息,请阅读指针。但基本上通过发送h作为指针,它将返回它的值为main。

#include <stdio.h> 

int findN(double xI, double xF, double h[]) { 
    int i = 0; 
    int n; 

    h[i] = 0.1; 
    do { 
     i++; 
     printf_s("%8.5f \n", *h); 
     n = ((xF - xI)/(*h)); 
     h[i] = h[i-1]/10; 


     printf_s("%6d \n", n); 
    } while (h[i] >= 0.00001); 


    return n; 
} 

int main() 
{ 
    double h[100]; 
    double xI = 1.0, xF = 1.0; 
    int n; 

    n = findN(xI, xF, h); 

    return 0; 
} 
+0

“h”的内存分配不存在。而这个例子目前并没有演示如何返回多个值。 – oklas

+0

@oklas C编程语言无法返回多个值。这就是为什么你需要使用指针。是的,h是在main()中声明的 – Archmede

+0

你声明'double * h = 0.1;'这个会用warn编译,'h'指针会被初始化为0,并且你试着像这样使用'* h =(* h)/ 10;' - 这将 - 分段错误 – oklas

1

典型方法其指针返回multpile值是使用阵列和传递给功能:

int f(double *h) { 
    h[0] = 1.1; 
    h[1] = 2.2; 
} 

int main() 
{ 
    // create pointer 
    double *h; 

    // initialize it with memory block 
    h = malloc(2*sizeof(double)); 

    // call the function 
    f(h); 

    // show output 
    printf_s("%8.5f \n", h[0]); 
    printf_s("%8.5f \n", h[1]); 

    // release memory block 
    free(h); 

    return 0; 
} 

此外同一阵列可以在没有存储器分配被创建。它更简单,但是只有在执行不会离开声明的函数范围时才存在数组。

int main() 
{ 
    // create array 
    double h[2]; 

    // call the function 
    f(h); 

    // show output 
    printf_s("%8.5f \n", h[0]); 
    printf_s("%8.5f \n", h[1]); 

    return 0; 
} 

如果你只能在函数知道元素的数叫你可以在功能分配数组和指针数组返回,在主叫方释放阵列。

double* f() { 
    // create pointer 
    double *h; 

    // some size calculations 
    int size = 1+1; 

    // initialize it with memory block 
    h = malloc(size*sizeof(double)); 

    // fill the array 
    h[0] = 1.1; 
    h[1] = 2.2; 

    // return array by pointer 
    return h; 
} 

int main() 
{ 
    // create pointer 
    double *h; 

    // call the function 
    h = f(); 

    // show output 
    printf_s("%8.5f \n", h[0]); 
    printf_s("%8.5f \n", h[1]); 

    // release memory block 
    free(h); 

    return 0; 
} 
0

有很多方法可以解决这个问题。另一个是返回struct

以下,findN()返回一个对象。只是碰巧该对象包含两个成员。这种方法适用于小物体时。对于大型物体,应考虑其他方法。

typedef struct { 
    int n; 
    double h; 
} nh; 

nh findN(double xI, double xF) { 
    nh retval; 
    retval.h = 0.1; 

    do { 
    printf_s("%8.5f\n", retval.h); 
    retval.n = ((xF - xI)/retval.h); 
    retval.h = retval.h/10; 
    printf_s("%6d\n", retval.n); 
    } while (retval.h >= 0.00001); 

    return retval; 
} 

// usage exanple 
nh y; 
y = findN(1.23, 4.56); 
printf_s("h:%8.5f, n:%6d\n", y.h, y.n); 
0

读指针,你将能够返回要返回尽可能多的价值,通过主调用函数时加在实际参数&小时,这意味着findN(XI,极,& h)和在声明函数findN在形式参数中添加double * h,即int findN(double xI,double xF,double * h)....“*的含义是-value在地址...的意思是&是地址。这个程序会在这个程序中进行全局变化,因为谷歌地址正在改变。你可以使用更多变量返回更多这样的值。这称为间接返回值。如果适用,请为我的答案投票。

0

处理此问题的最简单方法是将接受指向变量的指针的函数更改为接受值nh。然后这个函数将取消引用这些指针来更新调用函数中的相关变量。

void findN(double xI, double xF, int *ret_n, double *ret_h) 
{ 
    ... 
    *ret_n = n; 
    *ret_h = h; 
} 

然后,你可以调用你的函数是这样的:

int n; 
double h; 
findN(1.2, 3.4, &n, &h); 

这种方法是罚款数量相对较少的参数。如果参数个数太多,您可以创建一个struct,其中包含要返回的所有值,要么通过struct的地址传递,要么仅返回struct