C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了数组和指针的关系(区别)详解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我们知道,没有方括号和下标的数组@L_675_0@实际上代表数组的起始地址。这意味着数组@L_675_0@实际上就是一个指针。下面程序通过显示与间接运算符一起使用的数组@L_675_0@来说明这一点。
// This program shows an array name being dereferenced with the * operator.
#include <iostream>
using namespace std;

int main()
{
    short numbers[] = {10,20,30,40,50};
    cout << "The first element of the array is ";
    cout << *numbers << endl;
    return 0;
}
程序输出结果:

The first element of the array is 10

numbers 在上面程序中的作用类似于指向数组起始地址的指针,所以当 numbers 被解引用时,第一个元素被检索出来。那么,如何使用间接运算符来检索数组的全部内容呢?请记住,数组元素是一起存储在内存中的,如图 1 所示。


图 1 存储在内存中的数组元素

既然 numbers 是 numbers[0] 的地址,那么给 numbers 添加值岂不是就可以获得数组中其他元素的地址了?这样想当然很有道理,但是,这里有一个知识点非常重要,即:在数学语句中使用指针时,它不像常规变量那样工作。

C++ 中,当给一个指针添加一个值的时候,实际上添加的值是把这个值乘以指针引用的数据类型的大小。换句话说,如果给 numbers 加 1,实际上就是给 numbers 加上 1Xsizeof(short);如果给 numbers 加 2,实际上就是给 numbers + 2Xsizeof(short),以此类推。在 PC 上,这意味着以下说法是真实的,因为 short(短整数)通常使用 2 个字节:
  • *(numbers +1)是指地址 numbers + 1X2 处的值。
  • *(numbers + 2)是指地址 numbers + 2X2 处的值。
  • *(numbers + 3)是指地址numbers + 3X2 处的值。
以此类推。

这种自动转换意味着数组中的元素可以通过使用其下标或通过将下标添加到指向数组的指针来检索。既然表达式 *numbers 与 *(numbers + 0) 相同,它可以检索数组中的第一个元素,那么 *(numbers + 1) 就可以检索第二个元素。同样,*(numbers+2) 即可检索第三个兀素,以此类推。图 2 显示了下标表示法和指针表示法的等价性。


图 2 下标符号和对应的指针符号

注意,向指针添加值时,括号非常重要。* 运算符优先于 + 运算符,所以表达式 *numbers + 1 不等于 *(numbers + 1)。表达式 *numbers + 1 的意思是将数组的第一个元素的内容加 1, 而 *(numbers + 1) 则是先给 numbers 加 1,然后对其进行解引用。

下面的程序使用指针符号显示了被访问数组的整个内容
//This program processes an array using pointer notation.
#include <iostream>
using namespace std;

int main()
{
    const int SIZE = 5; // Size of the array
    int numbers[SIZE]; // Array of Integers
    // Get values to store in the array
    // Use pointer notation instead of subscripts
    cout << "Enter " << SIZE << " numbers: ";
    for (int count = 0; count < SIZE; count++)
        cin >> *(numbers + count);
    // Display the values in the array
    // Use pointer notation instead of subscripts
    cout << "Here are the numbers you entered:\n";
    for (int count = 0; count < SIZE; count++)
    cout << * (numbers + count) << " ";
    cout << endl;
    return 0;
}
程序输出结果:

Enter 5 numbers: 5 10 15 20 25
Here are the numbers you entered:
5 10 15 20 25

在使用数组时,请记住一个规则,即 arraY[index] 相当于 *(array + indeX),不仅如此,C++ 不会对数组执行边界检查。当使用指针遍历一个数组时,有可能会给指针一个越出数组边界的地址。

要理解数组@L_675_0@和指针之间的密切关系,请看下面的程序。它定义了一个 double 数组和一个 double 指针,该指针分配了数组的起始地址。随后,不仅指针符号可以与数组@L_675_0@一起使用,而且下标符号也可以与指针一起使用。
// This program uses subscript notation with a pointer
// variable and pointer notation with an array name.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    const int nUM_COINS = 5;
    double coins[NUM_COINS] = {0.05,0.1,0.25,0.5,1.0};
    double *doublePtr; // Pointer to a double
    // Assign the address of the coins array to doublePtr
    doublePtr = coins;
    // Display the contents of the coins array
    // Use subscripts with the pointer!
    cout << setprecision (2);
    cout << "Here are the values in the coins array:\n";
    for (int count = 0; count < NUM_COINS; count++)
        cout << doublePtr [count] << " ";
    // Display the contents of the coins array again,but this time use pointer notation with the array name!
    cout << "\nAnd here they are again:\n";
    for (int count = 0; count < NUM_COINS; count++)
        cout << *(coins + count) << " ";
    cout << endl;
    return 0;
}
程序输出结果:

Here are the values in the coins array:
0.05 0.1 0.25 0.5 1
And here they are again:
0.05 0.1 0.25 0.5 1

注意,当一个数组的地址分配给一个指针时,就不需要地址运算符了。由于数组的@L_675_0@已经是一个地址,所以使用 & 运算符是不正确的。但是,可以使用地址运算符来获取数组中单个元素的地址。

例如,&numbers[1] 得到 numbers[1] 的地址。在程序下面程序中就使用了该技巧。
// This program uses the address of each element in the array.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    const int nUM_COINS = 5;
    double coins[NUM_COINS] = {0.05,1.0};
    double *doublePtr; // Pointer to a double
    //Use the pointer to display the values in the array
    cout << setprecision (2);
    cout << "Here are the values in the coins array:\n";
    for (int count = 0; count < NUM_COINS; count++)
    {
        doublePtr = &coins[count];
        cout << *doublePtr << " ";
    }
    cout << endl;
    return 0;
}
程序输出结果:

Here are the values in the coins array:
0.05 0.1 0.25 0.5 1

数组@L_675_0@和指针变量的唯一区别是,不能改变数组@L_675_0@指向的地址。例如,假定存在以下定义:

double readings[20],@R_58_10586@ls[20];
double *dptr;

那么以下语句是合法的:

dptr = readings; // 使 dptr 指向 readings
dptr = @R_58_10586@ls; // 使 dptr 指向 @R_58_10586@ls

但是以下语句则是非法的:

readings = @R_58_10586@ls; // 非法!不能改变 readings
@R_58_10586@ls = dptr; // 非法!不能改变 @R_58_10586@ls

数组@L_675_0@是指针常量。不能让它们指向除了它们所代表的数组之外的任何东西。

大佬总结

以上是大佬教程为你收集整理的数组和指针的关系(区别)详解全部内容,希望文章能够帮你解决数组和指针的关系(区别)详解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: