简体   繁体   中英

Weird pointer syntax

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.

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].

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 be int main() . C++ has no "implicit int rule; you have to specify the return type explicitly.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM