I was looking at some code from google and something caught my eye.
#include <iostream>
using namespace std;
const int kStudents = 25;
const int kProblemSets = 10;
// This function returns the highest grade in the Problem Set array.
int get_high_grade(int *a, int cols, int row, int col) {
int i, j;
int highgrade = *a;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
if (*(a + i * cols + j) > highgrade) // How does this line work?
highgrade = *(a + i*cols + j);
return highgrade;
}
main() {
int grades[kStudents][kProblemSets] = {
{750, 700, 85, 720, 84},
{85, 92, 93, 96, 86},
{95, 90, 103, 76, 97},
{65, 62, 73, 84, 73}
};
int std_num = 4;
int ps_num = 5;
int highest;
cout << *(int *)grades << endl;
highest = get_high_grade((int *)grades, kProblemSets, std_num, ps_num);
cout << "The highest problem set score in the class is " << highest << endl;
}
It was (int *)grades. I've never seen that before in any tutorial before so it caught me off guard. I used cout << *(int *)grades << endl; to determine that it was a pointer but I've never seen a pointer with that syntax.
Can anyone explain how it works? Thanks.