Reputation: 121
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.
Upvotes: 0
Views: 201
Reputation: 263267
grades
is an array of array of int
objects.
The pointer expression grades
is implicitly converted in most contexts to a pointer to the array's initial (0th) element, so the expression grades
by itself is of type int(*)[kProblemSets]
, a pointer to an array of int
.
The (int*)
cast converts this pointer value from one pointer type to another, and the *
dereferences the resulting converted pointer, yielding an int
result.
This is questionable; I don't think there's any guarantee that the code will do what you expect. If the intent is to print the value of the first element of the first row of the array, just use:
std::cout << grades[0][0] << std::endl;
(Incidentally, main()
should beint main()
. C++ has no "implicit int
rule; you have to specify the return type explicitly.)
Upvotes: 2
Reputation: 6983
As grades is a 2d array, when cast to a pointer, you get a pointer to the first item of the array. This is what the (int*) does. The * at the front gets the value of the pointer - which as said, is the first item, which in this case has the value 750.
It's the equivalent to grades[0][0].
Upvotes: 2