簡體   English   中英

C Visual Studio 2017 中從函數到函數的動態數組

[英]Dynamics array from function to function in C visual studio 2017

我需要從一個函數轉移到另一個函數。 我像這樣嘗試:

    double toarray(double x,int n)
{
    double * results = new double[n];
    for (int j = 0; j < n; j++)
    {
        results[j] = function( x);
        x += jump;
    }
    return *results;
}

void showarr(double*arr[],int n,double x)
{
    for(int i=0;i<n;i++)
    {
        printf("Wynik dla x=" "%.2lf", x);
        if (denomcheck(x) == true && function(x)<INT_MAX)
        {
            printf(" | y=%.2lf \n", arr[i]);
        }
        else
            printf(" | MIANOWNIK = 0!!! \n");
        x += jump;
    } 
}

但是當我嘗試調用 toarray 作為 showarr 的變量時:

showarr(toarray(x, n), n, x);

我在 vs 2017 中收到 E0167 錯誤。

“參數類型“double”與參數類型“double **”不兼容

你的toarray應該返回一個指向數組的指針,而不是一個雙toarray值:

double *toarray(double x,int n) {
     double * results = new double[n];
     ...
     return results;
}

並且showarr應該處理雙精度數組(作為指向此類數組的指針傳遞,可以寫為double arr[]double * )而不是指向雙精度的指針數組(這就是double *arr[]的含義) :

void showarr(double arr[],int n,double x) {
    ...

此外,您應該刪除您創建的數組,即在之后不需要arr地方使用delete[] arr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM