为什么我这用的值传递,实参却改了
#include
#include<string>
using namespace std;
struct hero
{
string name;
int age;
string sex;
};
void bubbleSort(hero h[], int len)
{
hero temp = h[0];
for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (h[j].age > h[j + 1].age)
{
temp = h[j];
h[j] = h[j + 1];
h[j + 1] = temp;
}
}
}
}
int main()
{
hero h[5] =
{
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"},
};
int len = sizeof(h) / sizeof(h[0]);
for (int i = 0; i < len; i++)
{
cout << h[i].name << h[i].age << h[i].sex << endl;
}
bubbleSort(h, len);
for (int i = 0; i < len; i++)
{
cout << h[i].name << h[i].age << h[i].sex << endl;
}
system("pause");
}
数组作为参数,实际传递的是数组的首地址,相当于指针,所以函数内是可以修改数组元素值的