c - 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));
}
}
}
解决方案
您可以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));
推荐阅读
- jenkins-pipeline - jq 在 Jenkins 管道中没有将输出保存到变量
- javascript - Bootstrap 5 - 移动菜单未打开
- java - javax.servlet.ServletException: Servlet [dispatcher] 的 Servlet.init() 抛出异常
- r - 具有许多变量的回归,但不足以证明使用 . 并减去不必要的变量
- visual-studio-code - VS Code 停止在方法/函数/对象/等上显示 InlayHints 类型
- powershell - 如何在powershell的winforms日历控件中将背景颜色设置为特定日期?
- javascript - 检索所有客户的电子邮件地址条带 node.js
- php - PHP以状态1退出服务
- c - GTK得到多个范围值分割错误
- r - 如何正确输入说明以扩大 RMarkdown pdf.doc 页面的页边距