我在课堂上学习指针,但我从来没有100%理解数组。
我有一个作业,我必须从一个函数中读取学生成绩的数组。数组中的每个元素都有不同的分数,元素标识它是哪个学生。
我试图将分数读入数组,但当返回它时,我就迷失了方向。我试过google,但我更困惑了,因为人们都在谈论动态内存和使用新的和delete操作数,而我还没有做到这一点,我不想做错赋值。
我必须使用用户指针而不是索引来访问数组中的元素。下面的代码给出了错误"redefinition of formal parameter 'scoreArray'“
#include <iostream>
using namespace std;
const int maxStudents = 30;
double readScores(double[]);
int main()
{
    double scoreArray[maxStudents];
    readScores(scoreArray);
    cout<<scoreArray[1],scoreArray[2];
    system ("PAUSE");
    return 0;
}
double readScores(double scoreArray)
{
    double scoreArray[maxStudents];
    double *scorePTR;
    *scorePTR = scoreArray;
    for(int count = 0; count < maxStudents; count++)
    {
        cout<<"Please enter score for student "<<count+1<<" or -999 to end.\n";
        cin>>*(scorePTR+count);
        if(*(scorePTR+count) == -999)
        break;
    }
}发布于 2012-02-09 23:27:18
double readScores(double scoreArray)
{
    double scoreArray[maxStudents];
    ...
}编译错误的发生是因为您使用与形参double scoreArray同名的名称scoreArray声明了一个局部变量。更改其中一个的名称。
https://stackoverflow.com/questions/9213631
复制相似问题