c - 使用函数指针创建菜单驱动系统

标签 c pointers

我正在尝试使用函数指针来创建菜单驱动的系统,但是当我编译它时,我收到了警告

[Warning] initialization from incompatible pointer type 

void (*gradeProcess[4])(int)={printArray,minimum,maximum,average};

我无法解决这个问题,有人可以帮助我吗?

这是代码:

#include <stdio.h>
#define  STUDENTS 3
#define EXAMS 3

void printArray(const int grades[][EXAMS],int pupils,int tests);
void minimum(const int grades[][EXAMS],int pupils,int tests);
void maximum(const int grades[][EXAMS],int pupils,int tests);
void average(const int grades[][EXAMS],int pupils,int tests);

int main(void)
{
int examGrades[STUDENTS][EXAMS];
int choice;
int i,j;
void (*gradeProcess[4])(int)={printArray,minimum,maximum,average};

for(i=0;i<STUDENTS;i++){
    printf("Enter the grades of student %d:\n",i);
    for(j=0;j<EXAMS;j++){
        scanf("%d",&examGrades[i][j]);
    }
}

printf("\n\n0 to print array\n1 to find the lowest grade\n2 to find the highest grade\n3 to find average for each student\n4 to end program\n\n");

printf("Enter the number of operation you want to process:");
scanf("%d",&choice);

while(choice>=0 && choice<4){
    (*gradeProcess[choice])(choice);

    printf("Enter the number of operation you want to process:");
    scanf("%d",&choice);
}

printf("\n\nProgram execution completed\n");

system("PAUSE");
return (0);
}

void printArray(const int grades[][EXAMS],int pupils,int tests)
{
printf("You chose to print array\n\n");
printf("            [0]  [1]  [2]");

for(pupils=0;pupils<STUDENTS;pupils++){
    printf("\nstudent[%d]= ",pupils);
    for(tests=0;tests<EXAMS;tests++){
        printf("%4d",grades[pupils][tests]);
    }
}
}

void minimum(const int grades[][EXAMS],int pupils,int tests)
{
int lowest=0;

printf("The lowest grade will be displayed");

for(pupils=0;pupils<STUDENTS;pupils++){
    for(tests=0;tests<EXAMS;tests++){
        if(lowest>grades[pupils][tests]){
            lowest=grades[pupils][tests];
        }
    }
}

printf("\nThe lowest grade is %d",lowest);
}

void maximum(const int grades[][EXAMS],int pupils,int tests)
{
int highest=0;

printf("The highest grade will be displayed");

for(pupils=0;pupils<STUDENTS;pupils++){
    for(tests=0;tests<EXAMS;tests++){
        if(highest>grades[pupils][tests]){
            highest=grades[pupils][tests];
        }
    }
}

printf("\nThe highest grade is %d",highest);
}

void average(const int grades[][EXAMS],int pupils,int tests)
{
int total;
double average;

printf("You chose to display average of each student\n\n");

for(pupils=0;pupils<STUDENTS;pupils++){
    printf("The average for student[%d]:",pupils);
    for(tests=0;tests<EXAMS;tests++){
        total+=grades[pupils][tests];
    }
    average=(double)total/tests;
    printf("%.2lf",average);
}
}

最佳答案

帮自己一个忙,使用 typedef:

typedef void (*fptr)(const int [][EXAMS], int, int);

//...

fptr gradeProcess[4] = { printArray, minimum, maximum, average };

关于c - 使用函数指针创建菜单驱动系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7805805/

相关文章:

C++,将非指针类型分配给模板类的成员指针

c++ - C++ 中的这些 extern 声明有什么区别?

pointers - 如何定义一个函数,该函数接收一个指针作为参数并返回一个指向该参数的子节点之一的指针?

c - 结构不适用于多个 C 源文件

c - 删除通过引用传递给函数的链表中的节点

c++ - Busy-waiting和定时器中断在编程中的优缺点是什么?

c - 从类型 ‘__m128i’ 分配给类型 ‘int’ 时的类型不兼容

c - 指向字符串的指针数组(指针的基本类型)

c - 使用指向结构的指针数组期间出现段错误

cuda线程索引