user6089062
user6089062

Reputation: 21

how do i pass an array with a pointer into a function?

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)

Upvotes: 1

Views: 114

Answers (2)

Giuseppe Gorgoglione
Giuseppe Gorgoglione

Reputation: 329

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.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409166

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.

Upvotes: 1

Related Questions