Passing a pointer to an array in a function (C ++)

I am trying to pass an array to my calls to the build_max_heap and max_heapify functions, so I can change the array after each call, but I get the error message: "The candidate function is not viable: unknown conversion from" int [9] 'to' int * & 'for 1st argument. "

#include <iostream>
#include <string>
using namespace std;

void build_max_heap(int*& array, int size);
void max_heapify(int*& array, int size, int index);


void build_max_heap(int*& array, int size)
  {
      for(int i = size/2; i>=0; i--)
      {
          max_heapify(array, i);
      }
  }


void max_heapify(int*& array, int size, int index)
  {
      int leftChild = 2*index+1;
      int rightChild = 2*index+2;
      int largest;
      int heap_size = size;

      if( leftChild <= heap_size && array[leftChild] > array[index])
          largest = leftChild;
      else
          largest = index;

      if(rightChild <= heap_size && array[rightChild] > array[largest])
          largest = rightChild;

      if(largest != index) {
          int tempArray = array[index];
          array[index] = array[largest];
          array[largest] = tempArray;
          max_heapify(array, heap_size, largest);
      }

  }

int main()
{
      int array[]={5,3,17,10,84,19,6,22,9};
      int size = sizeof(array)/sizeof(array[0]);

      build_max_heap(array, size);

      return 0;
}
+4
source share
1 answer

int array[]={5,3,17,10,84,19,6,22,9};

array int*, , " " int*&, ( ). const :

void max_heapify(int* const& array, int size, int index)
//                    ^^^^^^

, ( ), : . const& , , std::string. ; , .

, :

void build_max_heap(int* array, int size)
void max_heapify(int* array, int size, int index)

max_heapify build_max_heap, :

void build_max_heap(int* array, int size)
{
   for(int i = size/2; i>=0; i--)
   {
       max_heapify(array, size, i);  // <-- 3 arguments
   }
}
+4

Source: https://habr.com/ru/post/1672610/


All Articles