在 C++ 中初始化指针数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38919859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:47:24  来源:igfitidea点击:

Initializing array of pointers in c++

c++arrays

提问by perkes456

I forgot how to initialize the array of pointers in c++ like following:

我忘记了如何在 C++ 中初始化指针数组,如下所示:

int * array[10]; 

Is this a proper solution like this? Here:

这是一个合适的解决方案吗?这里:

array = new int[10];

 // is this the correct way?? 

回答by Jean-Fran?ois Fabre

int * array[10];

defines 10 pointers on 10 int arrays statically

在 10 个 int 数组上静态定义 10 个指针

To go dynamic:

去动态:

int **array = new int *[10];

Better solution since you use C++: use std::vector

更好的解决方案,因为您使用 C++:使用 std::vector

std::vector<int *> v;
v.resize(10);
v[2] = new int[50];  // allocate one array

Since we're using vectors for the array of pointers, lets get rid of the pointers completelely

由于我们使用向量作为指针数组,让我们完全摆脱指针

std::vector<std::vector<int> > v;
v.resize(10);
v[2].resize(50);  // allocate one array

Then access the array like a matrix:

然后像矩阵一样访问数组:

v[3][40] = 14;

Going further, one way to initialize all the rows, using C++11, making a 10x50 int matrix in the end (but size can also change within the loop if we want). Needs gcc 4.9 and g++ -std=c++11to build

更进一步,使用 C++11 初始化所有行的一种方法,最后制作一个 10x50 的 int 矩阵(但如果我们愿意,大小也可以在循环内改变)。需要 gcc 4.9 并g++ -std=c++11构建

std::vector<std::vector<int> > v;
v.resize(10);
for (auto &it : v)
{
   it.resize(50);  // allocate arrays of 50 ints
}

回答by C. Flint

int **array = new int*[length];

Or, without dynamic memory allocaction :

或者,没有动态内存分配:

int *array[10];

回答by Vlad from Moscow

In general in most cases there is no great sense to initialize the array with exact addresses. You could assign the addresses or allocate appropriate memory during the usage of the array.

一般来说,在大多数情况下,用确切的地址初始化数组没有多大意义。您可以在使用数组期间分配地址或分配适当的内存。

Usually there is sense to initialize an array of pointers with null pointers. For example

通常用空指针初始化一个指针数组是有意义的。例如

int * array[10] = {};

If you want to declare the array and at once to allocate memory for each element of the array you could write for example

如果要声明数组并立即为数组的每个元素分配内存,则可以编写例如

int * array[10] = 
{ 
    new int, new int, new int, new int, new int, new int, new int, new int, new int, new int 
}; 

or

或者

int * array[10] = 
{ 
    new int( 0 ), new int( 1 ), new int( 2 ), new int( 3 ), new int( 4 ), new int( 5 ), new int( 6 ), new int( 7 ), new int( 8 ), new int( 9 ) 
}; 

But in any case it would be better to do the assignment using some loop or standard algorithm because in general the array can have more than 10 elements.

但无论如何,最好使用一些循环或标准算法进行分配,因为通常数组可以有 10 个以上的元素。

Also you should not forget to delete all allocated memory. For example

此外,您不应忘记删除所有分配的内存。例如

std::for_each( std::begin( array ), std::end(array ), std::default_delete<int>() );

Or if you have already defined objects of type int you could write for example

或者,如果您已经定义了 int 类型的对象,则可以编写例如

int x0, x1, x2, x3, x4, x5, x6, x7, x8, x9;
//...
int * array[10] = 
{ 
    &x0, &x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8, &x9 
}; 

Such an initialization is used very often for arrays of function pointers.

这种初始化经常用于函数指针数组。

回答by Zeokav

You can have something like this -

你可以有这样的东西 -

int **array = new int*[n](); //n is the number of pointers you need.
//() after declaration initializes all pointers with nullptr

回答by Garrigan Stafford

Firstint* array[10]would create an array of 10 Int pointers, which would be initlized to garbage values so best practice for that is

首先int* array[10]将创建一个包含 10 个 Int 指针的数组,该数组将被初始化为垃圾值,因此最佳实践是

int* array[10];
for(int i = 0;i<10;i++)
{
    array[i] = NULL;
}

That will safely unitize all pointers to an appropriate value so that if you try and access one that you haven't stored something in it will throw a seg fault every time.

这将安全地将所有指针统一到一个适当的值,以便如果您尝试访问一个您尚未在其中存储内容的指针,则每次都会引发段错误。

Secondarray = new int[10]is creating a pointer to a array of 10 integers which is a completely different thing. If you want to set up an array of pointers using newthat would be

其次array = new int[10]是创建一个指向 10 个整数数组的指针,这是完全不同的事情。如果你想使用new它来设置一个指针数组

int** array = new int*[10];

int** array = new int*[10];

回答by sorosh_sabz

ITNOA

ITNOA

you can use

您可以使用

int* a = {new int(....), new int(....) ,... 

or something like below

或类似下面的东西

for (int b = 0; b < 10; ++b)
 a[b] = new int(....);

I think this linkhave good explain about array of pointers.

我认为这个链接对指针数组有很好的解释。