• 数组和指针的关系(区别)详解

    我们知道,没有方括号和下标的数组名称实际上代表数组的起始地址。这意味着数组名称实际上就是一个指针。下面程序通过显示与间接运算符一起使用的数组名称来说明这一点。

    // 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 存储在内存中的数组元素

更多...

加载中...