here is my code:
#include <stdio.h>
#include <stdlib.h>
void AverageOfAss(float marks[][101], int numStudents, int numAss, float *avg[]) {
int i, j;
for (j=0; j<numAss; j++) {
*avg[j] = 0;
for (i=0; i<numStudents; i++) {
*avg[j] += marks[i][j];
*avg[j] = *avg[j]/(float)numStudents*100;
return ;
}
}
}
void PrintAvg(float *avg[], int numAss) {
int i;
for (i=0; i<numAss; i++) {
printf("Average for Exam %d = %.1f\n", i, *avg[i]);
return;
}
}
int main(int argc, char* argv[]) {
float grades[1001][101], avgAss[1001];
float *p;
int i, j, row, col;
p = avgAss;
row = atoi(argv[1]);
col = atoi(argv[2]);
FILE *input_grades;
input_grades = fopen("grades.txt", "r");
// READ IN GRADES
for (i=0; i<row; i++) {
for (j=0; j<col; j++) {
fscanf(input_grades, "%f, ", &grades[i][j]);
}
}
// OUTPUT GRADES LIST
printf("==================================\n");
printf("Student grades from the input file\n");
printf("==================================\n");
for (i=0; i<row; i++) {
printf("\n");
for (j=0; j<col; j++) {
printf("%.1f, ", grades[i][j]);
}
}
printf("\n\n");
AverageOfAss(grades, row, col, &avgAss);
PrintAvg(&avgAss, col);
fclose(input_grades);
}
Everything is alright except that when i tried to execute the code on the terminal, it showed some warnings:
incompatible pointer types passing 'float (*)[1001]' to parameter of type 'float **'
AverageOfAss(grades, row, col, &avgAss);
passing argument to parameter 'avg' here
void AverageOfAss(float marks[][101], int numStudents, int numAss, float *avg[]) {
incompatible pointer types passing 'float (*)[1001]' to parameter of type 'float **'
PrintAvg(&avgAss, col);
passing argument to parameter 'avg' here
void PrintAvg(float *avg[], int numAss) {
Does anyone know what happened to my codes? And how should i fix it? (newbie here)
The problem is that a pointer to an array of float
is not the same as a pointer to a pointer to a float
.
The error message actually tells you exactly how you should solve your problem... What the compiler is telling you is that &avgAss
is of type float(*)[1001]
while your argument is of type float **
. So you need to change your function argument type to match what the compiler expects, like e.g. float (*avg)[1001]
. Then you have a problem that *avg[j]
actually means *(avg[j])
and not what you need (*avg)[j]
.
However you don't need to pass a pointer to the array at all. All you need is to let the array naturally decay to a pointer to its first element, so what you really should do is to change the function argument to float *avg
and use it like a normal array avg[j]
. And of course don't pass a pointer to the array, use only avgAss
in the call.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments