C ++中的指针数组的语法 [英] Syntax of arrays of pointers in c++

查看:82
本文介绍了C ++中的指针数组的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c ++编程的新手,试图理解这种语法

I am new to c++ programming and trying to understand this syntax

int **arr;

arr = new int *[5];

我对这部分感到困惑 new int * [5] ;

I am confused about this part new int *[5]; Does it mean pointer of type int to 5 subpointers?

任何帮助将不胜感激。

推荐答案

希望当您逐篇阅读时会变得清楚:

This will hopefully become clear when you read it piece by piece:

new        int *             [5]
^^^        ^^^^^             ^^^
give me    pointers to int   and five of those.

然后将新指针中第一个指针的地址保存在 arr ,因此具有五个指针的动态分配数组。

Then you safe the address of the first of the new pointers in arr and thus have an dynamically allocated array of five pointers.

请注意,在现实世界中(即,如果没有老师/教授禁止您这样做, ),则应使用 std :: vector 而不是 new []

Note that "in the real world" (i.e. if no teacher/prof forbids you to) you would use an std::vector instead of new[].

这篇关于C ++中的指针数组的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆