使用指针调用函数时出错

时间:2015-02-11 19:30:16

标签: c++ arrays pointers

所以我仍然是C ++的新手,并且已经做了一段时间了。我想我正慢慢得到它,但不断收到错误“Intellisense:'*'的操作数必须是一个指针。”在第36行第10列。我需要做些什么来修复此错误?我将完成其他功能,因为我完成了每个功能,所以很抱歉额外的功能声明

// This program will take input from the user and calculate the 
// average, median, and mode of the number of movies students see in a month.

#include <iostream>
using namespace std;

// Function prototypes
double median(int *, int);
int mode(int *, int);
int *makeArray(int);
void getMovieData(int *, int);
void selectionSort(int[], int);
double average(int *, int);

// variables
int surveyed;



int main()
{
    cout << "This program will give the average, median, and mode of the number of movies students see in a month" << endl;
    cout << "How many students were surveyed?" << endl;
    cin >> surveyed;

    int *array = new int[surveyed];

    for (int i = 0; i < surveyed; ++i)
    {
        cout << "How many movies did student " << i + 1 << " see?" << endl;
        cin >> array[i];
    }



    median(*array[surveyed], surveyed);

}


double median(int *array[], int num)
{
    if (num % 2 != 0)
    {
        int temp = ((num + 1) / 2) - 1;
        cout << "The median of the number of movies seen by the students is " << array[temp] << endl;
    }
    else
    {
        cout << "The median of the number of movies seen by the students is " << array[(num / 2) - 1] << " and " << array[num / 2] << endl;
    }

}

1 个答案:

答案 0 :(得分:2)

问题:

  1. 以下行中使用的表达式*array[surveyed]

    median(*array[surveyed], surveyed);
    

    不对。 array[surveyed]是数组的surveyed - 元素。它不是指针。取消引用它是没有意义的。

  2. 声明中使用的median的第一个参数的类型与定义中使用的类型不同。宣言似乎是正确的。将实施更改为:

    double median(int *array, int num)
    
  3. 修正您拨打median的方式。而不是

    median(*array[surveyed], surveyed);
    

    使用

    median(array, surveyed);