RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 583150
Accepted
IgrikXD
IgrikXD
Asked:2020-10-26 23:46:53 +0000 UTC2020-10-26 23:46:53 +0000 UTC 2020-10-26 23:46:53 +0000 UTC

C++数组指针

  • 772

让我们有一个静态数组:

short tell[20];

在哪里:

  1. tell- 数组第一个元素的地址(2 字节块)。
  2. &tell- 整个数组的地址(40 字节块)。

并且有以下构造,指向一个包含 20 个 short 类型元素的数组:

short (*pas)[20] = &tell

pas 的结果数据类型是 type: short (*)[20]。实际上问题是如何创建这种类型的变量并使用 new 操作为其分配内存?

该示例取自以下书籍:Stephen Prata - The C++ Programming Language(第 6 版)。页 182.

c++
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Vlad from Moscow
    2020-10-27T00:35:36Z2020-10-27T00:35:36Z

    如果你有一个像这样的数组声明

    T a[N];
    

    其中T是某种类型,N是数组中元素的数量,则指向数组第一个元素的指针的类型为T *。例如

    T *p = a;
    

    在这个定义之后,指针p指向数组的第一个元素a。

    要为类似于上面定义的数组的数组动态分配内存,您可以编写

    T *p = new T[N];
    

    这里数组元素的类型为T,并且p如上所述指向动态分配的未命名数组的第一个元素。

    现在假设T这是类型的别名short[20],例如

    typedef short T[20];
    

    那么前面显示的指针声明可以写成

    T *p = &a;
    

    和

    T *p = new T[1];
    

    如果我们再次返回原始类型short[20],我们得到

    short( *p )[20] = &a;
    

    和

    short( *p )[20] = new short[1][20];
    

    最后一句的意思是分配一个元素的数组(你可以根据你的任务分配一个任意数量的元素的数组),其元素又是20个元素的数组类型short。

    请注意,当使用所谓的指针算法时,指针的值将更改为sizeof( T )

    因此,例如,如果您将指针声明为

    short( *p )[20] = &a;
    

    whereT等同于short[20],然后在应用之后,例如,对该指针的增量

    ++p;
    

    该指针将包含紧接在数组最后一个元素之后的地址a。

    好吧,最后是一个使用这种指针的例子。

    #include <iostream>
    #include <iomanip>
    #include <algorithm>
    #include <numeric>
    #include <iterator>
    
    const size_t N = 20;
    
    short ( * create_2D_array( size_t n ) )[N]
    {
        short ( *p )[N] = new short[n][N];
    
        return p;
    }   
    
    int main() 
    {
        short a[N];
    
        std::iota( std::begin( a ), std::end( a ), 1 );
    
        for ( short x : a ) std::cout << std::setw( 2 ) << x << ' ';
        std::cout << std::endl;
    
        short ( *p )[N] = create_2D_array( 1 );
    
        std::iota( std::reverse_iterator<short *>( *p + N ), 
                   std::reverse_iterator<short *>( *p ), 1 );
    
        for ( short x : *p ) std::cout << std::setw( 2 ) << x << ' ';
        std::cout << std::endl;
    
    
        std::cout << std::endl;
    
        std::swap( a, *p );
    
        for ( short x : a ) std::cout << std::setw( 2 ) << x << ' ';
        std::cout << std::endl;
    
        for ( short x : *p ) std::cout << std::setw( 2 ) << x << ' ';
        std::cout << std::endl;
    
        delete [] p;
    
        return 0;
    }
    

    将程序输出到控制台

     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 
    20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1 
    
    20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1 
     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 
    
    • 11
  2. Harry
    2020-10-27T00:01:27Z2020-10-27T00:01:27Z

    其实and tell, and &tell[0], and&tell都是同一个实体,数组分配内存块的起始地址...

    typedef short array[20];
    short * a = new array;
    

    这里我们描述了一个数组类型 20 short,并动态创建它,返回一个指向包含它的块开头的指针。我们像处理常规数组一样处理它,比如

    for(short i = 0; i < 20; ++i)
    {
        a[i] = i;
    }
    
    • 4

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5