0

Here's a fairly simple program that finds the max element of an 2d array grades and prints it out to the screen

#include <stdio.h>
const int t = 5;
int num_of_students;
int better(int grades[num_of_students][t], int num_of_students)
{
    int i, k; 
    int max = grades[0][0];
    
    for (i = 0; i < num_of_students; i++)
    {
        for (k = 0; k < t; k ++)
        {
            if (grades[i][k] > max)
            {
                max = grades[i][k];
            }
        }
    }
    return max;
}
int main(void)
{
    int i, k;
    printf("Give the number of students who took the test: ");
    scanf("%i", &num_of_students);
    int grades[num_of_students][t];

    for (i = 0; i < num_of_students; i++)
    {
        printf("Student %i\n", i+1);
        for (k = 0; k < t; k++)
       {
            printf("Give the score on test %i: ", k+1);
            scanf("%i", &grades[i][k]);
            while (grades[i][k] < 0 || grades[i][k] > 100)
            {
                printf("Not an acceptable score, try again %i: ", k+1);
                scanf("%i", &grades[i][k]);
            }
        }
    }
    int max = better(grades[num_of_students][t], num_of_students);
    printf("The best score is %i\n", max);
}

Yet when I'm trying to run the program the following errors pop up: test.c:47:45: warning: passing argument 1 of 'better' makes pointer from integer without a cast [-Wint-conversion] test.c:6:16: note: expected 'int (*)[(sizetype)t]' but argument is of type 'int'

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    `better(grades[num_of_students][t]` -> `better(grades`. Because you want to pass the entire array which is `grades`. `grades[num_of_students][t]` is a single element (and an out of bounds element at that). – kaylum Sep 02 '21 at 20:47
  • Don't read parameters from stdin, pass them as parameters: `int main(int argc, char **argv) { int num_of_students = argc > 1 ? strtoul(argv[1], NULL, 10) : 1; ...` – William Pursell Sep 02 '21 at 20:54

1 Answers1

1

For starters change the function declaration from

int better(int grades[num_of_students][t], int num_of_students)

to

int better(int num_of_students, int grades[num_of_students][t] )

Otherwise it is unclear whether in the declaration of the first parameter int grades[num_of_students][t] there is used the global variable num_of_students or the identifier of the second parameter. That is the function declaration as is will confuse readers of the code.

And call it like

int max = better( num_of_students, grades );

Otherwise you are trying to pass the non-existent element of the array grades[num_of_students][t] of the type int instead of the array itself.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • in GCC or CLANG it is possible to make a forward declaration of a parameter. So one can use: `int better(int num_students; int grades[num_of_students][t], int num_of_students) ` and later call `better(arr, num_of_students)` – tstanisl Oct 07 '21 at 09:53