文章目录
- 目录
- 1.指针
- 2.引用
目录
1.指针
#include <iostream>using namespace std;int main ()
{int var1;char var2[10];cout << "var1 变量的地址: ";cout << &var1 << endl;cout << "var2 变量的地址: ";cout << &var2 << endl;return 0;
}
var1 变量的地址: 0x7ffd8696414c
var2 变量的地址: 0x7ffd86964150
#include <iostream>using namespace std;int main ()
{int var = 20; // 实际变量的声明int *ip; // 指针变量的声明ip = &var; // 在指针变量中存储 var 的地址cout << "Value of var variable: ";cout << var << endl;// 输出在指针变量中存储的地址cout << "Address stored in ip variable: ";cout << ip << endl;// 访问指针中地址的值cout << "Value of *ip variable: ";cout << *ip << endl;return 0;
}
Value of var variable: 20
Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20
#include <iostream>using namespace std;
const int MAX = 3;int main ()
{int var[MAX] = {10, 100, 200};int *ptr;// 指针中的数组地址ptr = var;for (int i = 0; i < MAX; i++){cout << "Address of var[" << i << "] = ";cout << ptr << endl;cout << "Value of var[" << i << "] = ";cout << *ptr << endl;// 移动到下一个位置ptr++;}return 0;
}
Address of var[0] = 0xbfa088b0
Value of var[0] = 10
Address of var[1] = 0xbfa088b4
Value of var[1] = 100
Address of var[2] = 0xbfa088b8
Value of var[2] = 200
#include <iostream>using namespace std;
const int MAX = 3;int main ()
{int var[MAX] = {10, 100, 200};int *ptr;// 指针中第一个元素的地址ptr = var;int i = 0;while ( ptr <= &var[MAX - 1] ){cout << "Address of var[" << i << "] = ";cout << ptr << endl;cout << "Value of var[" << i << "] = ";cout << *ptr << endl;// 指向上一个位置ptr++;i++;}return 0;
}
Address of var[0] = 0xbfce42d0
Value of var[0] = 10
Address of var[1] = 0xbfce42d4
Value of var[1] = 100
Address of var[2] = 0xbfce42d8
Value of var[2] = 200
然而,指针和数组并不是完全互换的。例如,请看下面的程序:
#include <iostream>using namespace std;
const int MAX = 3;int main ()
{int var[MAX] = {10, 100, 200};for (int i = 0; i < MAX; i++){*var = i; // 这是正确的语法var++; // 这是不正确的}return 0;
}
把指针运算符 * 应用到 var 上是完全可以的,但修改 var 的值是非法的。这是因为 var 是一个指向数组开头的常量,不能作为左值。
由于一个数组名对应一个指针常量,只要不改变数组的值,仍然可以用指针形式的表达式。例如,下面是一个有效的语句,把 var[2] 赋值为 500:
*(var + 2) = 500;
上面的语句是有效的,且能成功编译,因为 var 未改变
#include <iostream>using namespace std;
const int MAX = 3;int main ()
{int var[MAX] = {10, 100, 200};int *ptr[MAX];for (int i = 0; i < MAX; i++){ptr[i] = &var[i]; // 赋值为整数的地址}for (int i = 0; i < MAX; i++){cout << "Value of var[" << i << "] = ";cout << *ptr[i] << endl;}return 0;
}
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
#include <iostream>using namespace std;int main ()
{int var;int *ptr;int **pptr;var = 3000;// 获取 var 的地址ptr = &var;// 使用运算符 & 获取 ptr 的地址pptr = &ptr;// 使用 pptr 获取值cout << "Value of var :" << var << endl;cout << "Value available at *ptr :" << *ptr << endl;cout << "Value available at **pptr :" << **pptr << endl;return 0;
}
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
#include <iostream>
#include <ctime>using namespace std;
void getSeconds(unsigned long *par);int main ()
{unsigned long sec;getSeconds( &sec );// 输出实际值cout << "Number of seconds :" << sec << endl;return 0;
}void getSeconds(unsigned long *par)
{// 获取当前的秒数*par = time( NULL );return;
}
2.引用
#include <iostream>using namespace std;int main ()
{// 声明简单的变量int i;double d;// 声明引用变量int& r = i;double& s = d;i = 5;cout << "Value of i : " << i << endl;cout << "Value of i reference : " << r << endl;d = 11.7;cout << "Value of d : " << d << endl;cout << "Value of d reference : " << s << endl;return 0;
}
Value of i : 5
Value of i reference : 5
Value of d : 11.7
Value of d reference : 11.7
#include <iostream>
using namespace std;// 函数声明
void swap(int& x, int& y);int main ()
{// 局部变量声明int a = 100;int b = 200;cout << "交换前,a 的值:" << a << endl;cout << "交换前,b 的值:" << b << endl;/* 调用函数来交换值 */swap(a, b);cout << "交换后,a 的值:" << a << endl;cout << "交换后,b 的值:" << b << endl;return 0;
}// 函数定义
void swap(int& x, int& y)
{int temp;temp = x; /* 保存地址 x 的值 */x = y; /* 把 y 赋值给 x */y = temp; /* 把 x 赋值给 y */return;
}
交换前,a 的值: 100
交换前,b 的值: 200
交换后,a 的值: 200
交换后,b 的值: 100
#include <iostream>using namespace std;double vals[] = {10.1, 12.6, 33.1, 24.1, 50.0};double& setValues( int i )
{return vals[i]; // 返回第 i 个元素的引用
}// 要调用上面定义函数的主函数
int main ()
{cout << "改变前的值" << endl;for ( int i = 0; i < 5; i++ ){cout << "vals[" << i << "] = ";cout << vals[i] << endl;}setValues(1) = 20.23; // 改变第 2 个元素setValues(3) = 70.8; // 改变第 4 个元素cout << "改变后的值" << endl;for ( int i = 0; i < 5; i++ ){cout << "vals[" << i << "] = ";cout << vals[i] << endl;}return 0;
}
改变前的值
vals[0] = 10.1
vals[1] = 12.6
vals[2] = 33.1
vals[3] = 24.1
vals[4] = 50
改变后的值
vals[0] = 10.1
vals[1] = 20.23
vals[2] = 33.1
vals[3] = 70.8
vals[4] = 50