1

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:

  1. incompatible pointer types passing 'float (*)[1001]' to parameter of type 'float **'

    AverageOfAss(grades, row, col, &avgAss);

  2. passing argument to parameter 'avg' here

    void AverageOfAss(float marks[][101], int numStudents, int numAss, float *avg[]) {

  3. incompatible pointer types passing 'float (*)[1001]' to parameter of type 'float **'

    PrintAvg(&avgAss, col);

  4. 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)

2
  • Unrelated, that is not going to calculate the averages correctly even after fixing the parameter types on both ends of the calls. And both the calculation for-loop and the reporting for-loop have return; buried in the body of the loops. I doubt you desired them to only iterate once, then return to the caller. Commented Mar 20, 2016 at 10:22
  • that is exactly what im facing right now, like the code works, but the average it calculated is wrong and i have no idea how it got the number. do you know what the the problem with my codes? Commented Mar 20, 2016 at 19:39

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to pass an array as argument to a function, modify the content of the array in that function and retain the modified array, there is no need to pass a pointer to a pointer to the array, just pass the pointer to the array (i.e. the name of the array without the [] or & operators). In C, data of basic types are passed to functions by copy, so you need to pass pointers to them to retain the modifications. Arrays cannot be passed by copy and one level of indirection is enough.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.