首页 > 解决方案 > Called object type 'void' is not a function or function pointer

问题描述

This program is designed to essentially take user input of grades and output various functions listed. The main problem I have with this is that my pointer (processGrades) to functions will not work. It outputs the error "Called object type 'void' is not a function or function pointer". Is this not the correct implementation of pointers? What might be causing this?

#include <stdio.h>

void minimum(int students, int exams, int studentGrades[students][exams])
{
    int lowGrade = 100;
    size_t i, j;
    for (i = 0; i < students; ++i) {
        for (j = 0; j < exams; ++j) {
            if (studentGrades[i][j] < lowGrade) {
                lowGrade = studentGrades[i][j];
            }
        }
    }
    printf("Minimum grade : %d\n", lowGrade); // return minimum grade
    return;
}

void maximum(int students, int exams, int studentGrades[students][exams])
{
    int highGrade = 0; // initialize to lowest possible grade
    size_t i, j;
    for (i = 0; i < students; ++i) {
        for (j = 0; j < exams; ++j) {
            if (studentGrades[i][j] > highGrade) {
                highGrade = studentGrades[i][j];
            }
        }
    }
    printf("Maximum grade : %d\n", highGrade); // return maximum grade
    return;
}

void average(int students, int exams, int studentGrades[students][exams])
{
    double total = 0.0; // sum of test grades
    size_t i, j;
    for (i = 0; i < students; ++i) {
        for (j = 0; j < exams; ++j) {
            total += studentGrades[i][j];
        }
        printf ("Average of student %lu : %f\n", i, total / exams); // average
        total = 0;
    }
    return;
}

void printArray(int students, int exams, int studentGrades[students][exams])
{
    size_t i, j;
    printf("%s", " [0] [1] [2] [3]");
    for (i = 0; i < students; ++i) {
        printf("\nstudentGrades[%lu] ", i);
        for (j = 0; j < exams; ++j) {
            printf("%-5d", studentGrades[i][j]);
        }
    }
    printf("\n");
    return;
}

int main(void)
{
    int choice;
    int students;
    int exams;
    printf("Let's create a 2Dim array!\n\n");
    printf("How many students? ");
    scanf("%d", &students);
    printf("\nHow many exams? ");
    scanf("%d", &exams);
    printf("\n");
    int studentGrades[students][exams];
    int i;
    int j;
    int entry;
    for (i = 0; i < students; i++){
        for (j=0; j < exams; j++) {
            printf("enter [%d] [%d]: ", i,j);
            scanf("%d",&entry);
            studentGrades[i][j] = entry;
        }
    }
    void (*processGrades[4]);
    processGrades[0] = printArray;
    processGrades[1] = minimum;
    processGrades[2] = maximum;
    processGrades[3] = average;
    int stop = 0;
    while(stop != 1) {
    printf("Enter a choice:\n");
    printf("0 Print the array of grades\n");
    printf("1 Find the minimum grade\n");
    printf("2 Find the maximum grade\n");
    printf("3 Print the average on all tests for each student\n");
    printf("4 End Program\n");
    scanf("%d", &choice);
        if(choice == 4){
            stop = 1;
        }
        else{
        (*processGrades[choice])(students)(exams)(studentGrades));
    }
}
}

标签: c

解决方案


您可以typedef使用流程函数类型的函数指针:

typedef void (*processGradesFnPtr)(int, int, int [*][*]);

在 中main,您可以声明一个包含四个函数指针的数组并相应地分配它们。

processGradesFnPtr processGrades[4];
processGrades[0] = printArray;
processGrades[1] = minimum;
processGrades[2] = maximum;
processGrades[3] = average;

当你打电话给他们时,你必须这样称呼他们:

(*processGrades[choice])(students, exams, (studentGrades));

推荐阅读