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

user6089062

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)

Some programmer dude

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How do I use bind to pass a member function as a function pointer?

From Dev

How do I pass in a parameter to a function in an array?

From Dev

How do i change the pointer of a variable pass into a function

From Dev

How do I pass a pointer to a multidimensional array in C?

From Dev

How do I dereference a pointer that is an element of an array that is passed into a function

From Dev

How to pass pointer to array of byte to function?

From Dev

How do I pass an array or list as a parameter to a PowerShell function?

From Dev

How do I pass an array to a function in Rust and change its content?

From Dev

How do I pass a list of values to a function expecting an array?

From Dev

How do I pass an address of array of structs to a function?

From Dev

How do I pass an array or list as a parameter to a PowerShell function?

From Dev

How do I pass a string array from a function to main

From Dev

How do I pass $(this) to a function?

From Dev

C++: How do I pass a pointer to a member function of another class?

From Dev

Why can't I pass the address of a pointer to a fixed size array into a function expecting a pointer to a pointer in C?

From Dev

How can I pass a function object to a function that expects a void * pointer

From Dev

How do I return a function pointer as const?

From Dev

How do I create a noexcept function pointer?

From Dev

How do I return a function pointer as const?

From Dev

How do I call a function with pointer variable?

From Dev

Pass char pointer array to function

From Dev

Pass char pointer/array to a function

From Dev

How to pass a float array to the function that has unsigned char pointer as an argument?

From Dev

How to pass a pointer to a function argument of type fixed size array?

From Dev

How can I pass a member function pointer into a function that takes a regular function pointer?

From Dev

How do you pass an object through a function using a pointer?

From Dev

how do i access an array returned as a pointer from a function using a subscript?

From Dev

How should I pass a pointer-to-member-function to setMouseCallback in OpenCV?

Related Related

  1. 1

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

  2. 2

    How do I use bind to pass a member function as a function pointer?

  3. 3

    How do I pass in a parameter to a function in an array?

  4. 4

    How do i change the pointer of a variable pass into a function

  5. 5

    How do I pass a pointer to a multidimensional array in C?

  6. 6

    How do I dereference a pointer that is an element of an array that is passed into a function

  7. 7

    How to pass pointer to array of byte to function?

  8. 8

    How do I pass an array or list as a parameter to a PowerShell function?

  9. 9

    How do I pass an array to a function in Rust and change its content?

  10. 10

    How do I pass a list of values to a function expecting an array?

  11. 11

    How do I pass an address of array of structs to a function?

  12. 12

    How do I pass an array or list as a parameter to a PowerShell function?

  13. 13

    How do I pass a string array from a function to main

  14. 14

    How do I pass $(this) to a function?

  15. 15

    C++: How do I pass a pointer to a member function of another class?

  16. 16

    Why can't I pass the address of a pointer to a fixed size array into a function expecting a pointer to a pointer in C?

  17. 17

    How can I pass a function object to a function that expects a void * pointer

  18. 18

    How do I return a function pointer as const?

  19. 19

    How do I create a noexcept function pointer?

  20. 20

    How do I return a function pointer as const?

  21. 21

    How do I call a function with pointer variable?

  22. 22

    Pass char pointer array to function

  23. 23

    Pass char pointer/array to a function

  24. 24

    How to pass a float array to the function that has unsigned char pointer as an argument?

  25. 25

    How to pass a pointer to a function argument of type fixed size array?

  26. 26

    How can I pass a member function pointer into a function that takes a regular function pointer?

  27. 27

    How do you pass an object through a function using a pointer?

  28. 28

    how do i access an array returned as a pointer from a function using a subscript?

  29. 29

    How should I pass a pointer-to-member-function to setMouseCallback in OpenCV?

HotTag

Archive