在 c++ 中传递数组的 through 函数,在 ']' 之前得到错误:primary-expression

Passing array's through functions in c++, getting error:primary-expression before ']'

本文关键字:错误 primary-expression c++ through 数组 函数      更新时间:2023-10-16

我正在上基础csci,计算机编程课程,并且已经花了几个小时摆弄这些代码。我试图通过一个函数传递数组,我的代码将无法编译。我不知道我的代码出了什么问题。内容如下:

int buildArray (double*);
int main ()
{
    int valuesPerLine;
    int randomValues;
    double array[110];
    srand(time(NULL));
    cout<<"How many values should be displayed per line? ";
    cin>>valuesPerLine;
    randomValues=buildArray(array[]);
    cout<<array[50];
    return (0);
}
int buildArray (double array[])
{
    int t; //t is the total number of numbers in the array
    t=rand();
    array[t];
    for (int i=0; i<t; i++)
        {
            array[i]=randDouble();
        }
    return(t);
}

cout<<array[50];只是为了我自己看看答案是否改变。它不会出现在最终代码中。

有什么简单的我错过了吗?我通常能够在课堂上帮助其他人编写代码,但由于某种原因,我不能弄清楚这个。

谢谢你做的一切!注:这不是完整的代码,我知道在这种情况下,我没有说randDouble是什么,但我不认为这很重要,因为代码似乎编译得很好。如果需要,请告诉我,我可以贴在下面。

将数组传递给函数时,只传递指向数组第一个元素的指针。

在你的例子中,它应该是:

randomValues = buildArray(array);