previous | start | next

17. Passing Array Parameters

Here is a function that sorts n integers in the array passed to a:

 void sort(int a[], int n)
 {
   for(int len = n; len > 1; len--) {
     int maxpos = 0;
     for(int i = 1; i < len; i++) {
        if ( a[i] > a[maxpos] ) {
           maxpos = i;
        }
     }
     int tmp = a[maxpos];
     a[maxpos] = a[len-1];
     a[len-1] = tmp;
   }
 }    

But in C++ the way an array is passed is by passing the address of the first element. So the type of the receiving parameter is just a pointer and the sort function could equivalent be declared like this:

 void sort(int *a, int n);          
         


previous | start | next