//堆区开辟数组
int main() {
int* arr = new int[10];
for (int i = 0; i < 10; i++)
{
arr[i] = i + 100;
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << endl;
}
//释放数组 delete 后加 []
delete[] arr;
system("pause");
return 0;
}
int arry[10];
其实这个arry他就是arry[0]的地址,所以创建的指针
int * arry = new [10];
可以arry[]直接读,在之前数组传参的时候也讲了,指针会自动退化为地址。
void mySwap01(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << “1. 1 a ,b 的地址”<< endl;
cout << int(&a) << endl;
cout << int(&b) << endl;
}
//2. 地址传递
void mySwap02(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
cout << “1 . 2 a ,b 的地址” << endl;
cout << int(a) << endl;
cout << int(b) << endl;
}
//3. 引用传递
void mySwap03(int& a, int& b) {
int temp = a;
a = b;
b = temp;
cout << “1 . 3 a ,b 的地址” << endl;
cout << int(&a) << endl;
cout << int(&b) << endl;
}
int main() {
int a = 10;
int b = 20;
cout << "1,a ,b 的地址" << endl;
cout << int(&a) << endl;
cout << int(&b) << endl;
mySwap01(a, b);
//cout << "a:" << a << " b:" << b << endl;
mySwap02(&a, &b);
//cout << "a:" << a << " b:" << b << endl;
mySwap03(a, b);
//cout << "a:" << a << " b:" << b << endl;
system("pause");
return 0;
}