2017-04-12 68 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