C 使用函数指针创建菜单驱动系统
我试图使用指向函数的指针来创建一个菜单驱动的系统,但是当我编译它时,我得到了一个警告C 使用函数指针创建菜单驱动系统,c,pointers,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 v
[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);
}
}
#包括
#定义学生3
#定义考试3
void printary(常量整数分数[][考试]、整数学生、整数测试);
无效最小值(常量整数分数[][考试]、整数学生、整数测试);
无效最大值(常量整数分数[][考试]、整数学生、整数测试);
无效平均值(常量整数分数[][考试]、整数学生、整数测试);
内部主(空)
{
国际考试[学生][考试];
智力选择;
int i,j;
void(*gradeProcess[4])(int)={printArray,minimum,maximum,average};
对于(i=0;i帮你自己一个忙,使用typedef:
typedef void (*fptr)(const int [][EXAMS], int, int);
//...
fptr gradeProcess[4] = { printArray, minimum, maximum, average };
要使用函数指针的函数都有签名void foo(const int[][tests],int,int)
,但您声明的函数指针用于签名void foo(int)
,因此您会收到此警告。请从void(*gradeProcess[4])(int)修改声明
tovoid(*gradeProcess[4])(常量int[][tests],int,int)
匹配签名。更好的选项是Kerrek SB的建议。建议您遵循提供的typedef
的建议。完成后,您需要更改使用函数指针的方式。可能您打算使用(*gradeProcess[choice])(examGrades、学生、考试)
或(*成绩处理[选择])((常量int(*)(考试))考试,学生,考试);
或成绩处理[选择]((常量int(*)(考试))考试,学生,考试);
此外,还可以查看最低和最高值函数的初始化逻辑,即最小值和最大值。此外,在平均值函数中,您使用的是应已初始化的未初始化变量总和。
希望这有帮助