使用指向函数的指针来创建菜单驱动的系统

时间:2011-10-18 10:27:47

标签: 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);
}
}

2 个答案:

答案 0 :(得分:1)

帮自己一个忙,使用typedef:

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

//...

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

答案 1 :(得分:0)

您要使用函数指针的函数都具有void foo(const int [][EXAMS], int, int)的签名,但您声明的函数指针用于签名void foo(int),因为您将收到此警告。将声明从void (*gradeProcess[4])(int)修改为void (*gradeProcess[4])(const int [][EXAMS], int, int)以匹配签名。更好的选择将是Kerrek SB所建议的。建议您遵循提供的typedef建议。完成后,您需要更改使用函数指针的方式。您可能打算使用(*gradeProcess[choice])(examGrades, STUDENTS, EXAMS);(*gradeProcess[choice]) ( (const int (*)[EXAMS])examGrades, STUDENTS, EXAMS);gradeProcess[choice]( (const int (*)[EXAMS])examGrades, STUDENTS, EXAMS); 另外,查看最低和最低初始化的逻辑。你最低功能和最低功能最大值。同样在平均函数中,您使用的是未初始化的变量total,它应该已经初始化 希望这有帮助!