I have a small lab where I'm trying to dynamically allocate an array to hold whatever number of scores. We were given the code to pass it into a function that sorts those scores via pointer notation. However, when I try to pass the array into the function, I get the above error. I did some googling and found that when you pass an array by pointer notion, you can't use the [] in the function, only *. Once I take the brackets out and just leave int * however, then I get a few errors inside the sort function itself, mostly ref to []. Was the sort function written wrong on purpose or am I passing nums wrong?

Thanks.

//function prototypes
void arrSelectSort(int *[], int);

int main()
{
    //dynamically allocate an array
    int *nums;
    int size;
    int x; 
    float total;
    
    //get number of test scores
    cout<<"How many test scores would you like to enter? ";
    cin>>size;
    
    //dynamically allocate an array large enough to hold 
    //that many test scores
    nums = new int[size];
    
    //get test scores
    for (x = 0;x < size;x++)
    {
        cout<< "Score " << (x + 1) << ": ";
        cin>>nums[x];
    
    } 
    
    //calculate total test scores 
    for (x = 0;x <size;x++)
    {
        
        total += nums[x];
        
    }
    
    //sort array
    arrSelectSort(nums, size);
    
    
    //display results
    cout<<total<<endl;
    
    //display array
    for (x = 0;x <size;x++)
    {
        
        cout<<nums[x];
        
    }
    
    
    
    return 0;
    
}

void arrSelectSort(int *arr[], int size)
{
   int startScan, minIndex;
   int *minElem;

   for (startScan = 0; startScan < (size - 1); startScan++)
   {
      minIndex = startScan;
      minElem = arr[startScan];
      for(int index = startScan + 1; index < size; index++)
      {
         if (*(arr[index]) < *minElem)
         {
            minElem = arr[index];
            minIndex = index;
         }
      }
      arr[minIndex] = arr[startScan];
      arr[startScan] = minElem;
   }
}

1. Correct function body, of course. Absolutely clear declaration:

void arrSelectSort(int arr[], int size);
// the same as
void arrSelectSort(int *arr, int size);

2. It seems you forgot to initialize total var...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.