C++ 指向数组的指针

C++ 教程 · 2019-02-25 18:13:03

您可以先跳过本章,等了解了 C++ 指针的概念之后,再来学习本章的内容。

如果您对 C++ 指针的概念有所了解,那么就可以开始本章的学习。数组名是一个指向数组中第一个元素的常量指针。因此,在下面的声明中:

double balance[50];

balance 是一个指向 &balance[0] 的指针,即数组 balance 的第一个元素的地址。因此,下面的程序片段把 p 赋值为 balance 的第一个元素的地址:

double *p;
double balance[10];

p = balance;

使用数组名作为常量指针是合法的,反之亦然。因此,*(balance + 4) 是一种访问 balance[4] 数据的合法方式。

一旦您把第一个元素的地址存储在 p 中,您就可以使用 *p、*(p+1)、*(p+2) 等来访问数组元素。下面的实例演示了上面讨论到的这些概念:

实例

#include <iostream> using namespace std; int main () { // 带有 5 个元素的双精度浮点型数组 double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0}; double *p; p = balance; // 输出数组中每个元素的值 cout << "使用指针的数组值 " << endl; for ( int i = 0; i < 5; i++ ) { cout << "*(p + " << i << ") : "; cout << *(p + i) << endl; } cout << "使用 balance 作为地址的数组值 " << endl; for ( int i = 0; i < 5; i++ ) { cout << "*(balance + " << i << ") : "; cout << *(balance + i) << endl; } return 0; }

当上面的代码被编译和执行时,它会产生下列结果:

使用指针的数组值
*(p + 0) : 1000
*(p + 1) : 2
*(p + 2) : 3.4
*(p + 3) : 17
*(p + 4) : 50
使用 balance 作为地址的数组值
*(balance + 0) : 1000
*(balance + 1) : 2
*(balance + 2) : 3.4
*(balance + 3) : 17
*(balance + 4) : 50

在上面的实例中,p 是一个指向 double 型的指针,这意味着它可以存储一个 double 类型的变量。一旦我们有了 p 中的地址,*p 将给出存储在 p 中相应地址的值,正如上面实例中所演示的。

点击查看所有 C++ 教程 文章: https://codercto.com/courses/l/18.html

查看所有标签

Iterative Methods for Sparse Linear Systems, Second Edition

Iterative Methods for Sparse Linear Systems, Second Edition

Yousef Saad / Society for Industrial and Applied Mathematics / 2003-04-30 / USD 102.00

Tremendous progress has been made in the scientific and engineering disciplines regarding the use of iterative methods for linear systems. The size and complexity of linear and nonlinear systems arisi......一起来看看 《Iterative Methods for Sparse Linear Systems, Second Edition》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具