c++ - 使用指针调用函数时出错

标签 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. 表达式 *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);
    

关于c++ - 使用指针调用函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28462889/

相关文章:

c++ - 在 C++ 4.8.1 中使用 strlen 的打印输出时出现段错误

arrays - 过滤一个数组以求另一个数组的总和

php - 如何将我的数组分成 3 个元素的组,按最后一个元素对它们进行排序并显示元素?

c - 为什么这个带有指针的 for 循环不会去任何地方?

Qt 库推荐使用 C++ Qt Creator 版本 4.8.5

c++ - 如何获取应用程序的所有鼠标事件?

ios - 从 'int *' 分配到 'int' 的指针转换不兼容的整数

c - 在 Arduino 上使用 C 中 PROGMEM 中的结构函数指针

c++ - 给定一个整数 N,按字典顺序打印从 1 到 N 的数字

arrays - 颠簸变换: Make the nested object field to be part of main Json Object and also convert the nested object to String