Reputation: 55
I'm trying to use pointers to functions to create a menu-driven system, but when I compile it I got a warning
[Warning] initialization from incompatible pointer type
void (*gradeProcess[4])(int)={printArray,minimum,maximum,average};
I couldn't fix it, can anyone help me?
Here is the code:
#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);
}
}
Upvotes: 0
Views: 3264
Reputation: 11395
The functions for which you want to use function pointers all have a signature of void foo(const int [][EXAMS], int, int)
but the function pointer you have declared is for signature void foo(int)
due to which you are getting this warning. Modify the declarations from void (*gradeProcess[4])(int)
to void (*gradeProcess[4])(const int [][EXAMS], int, int)
to match the signature. The better option will be what Kerrek SB has suggested. It is recommended you follow suggestion of typedef
provided. Once you have done that you need to change the way you are using your function pointer. Probably you meant to use (*gradeProcess[choice])(examGrades, STUDENTS, EXAMS);
or (*gradeProcess[choice]) ( (const int (*)[EXAMS])examGrades, STUDENTS, EXAMS);
or gradeProcess[choice]( (const int (*)[EXAMS])examGrades, STUDENTS, EXAMS);
Additionally look into the logic of initialization of lowest & highest you functions minimum & maximum. Also in average function you are using uninitialized variable total which should have been initialized.
Hope this helps!
Upvotes: 0
Reputation: 477338
Do yourself a favour, use a typedef:
typedef void (*fptr)(const int [][EXAMS], int, int);
//...
fptr gradeProcess[4] = { printArray, minimum, maximum, average };
Upvotes: 1