#include<iostream>
using namespace std;
void mySort(int*&a, int n)
{
a = new int[n];
int i;
for (i = 0; i < n; i++)
{
cin >> a[i];
}
int j , temp;
for (i = 0; i < n-1 ;i++)
{
for (j = 0; j < n-1-i; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
int main()
{
int i, n;
int* a = 0;
cin >> n;
mySort(a, n);
for (i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
delete[] a;
return 0;
}
int*& a
表示a
是一个引用,其所引用对象的类型是int*
,引用是另一个变量的别名。因此在mySort()
中对a
的修改就是对main函数中变量a
的修改