C++ 指针数组声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16053643/
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
C++ Pointer array declaration
提问by Split
Currently I have a several classes with an array defined as 'float myIDs'. I want to move the variable into my parent class and change it to a pointer ('float * myIDs').
目前我有几个类,其中的数组定义为“float myIDs”。我想将变量移动到我的父类中并将其更改为指针('float * myIDs')。
Currently I'm declaring its values like this:
目前我声明它的价值是这样的:
float myIDs[] = {
//Variables
};
As its now a pointer, I thought that it would be roughly the same:
作为它现在的指针,我认为它会大致相同:
myIDs = new float[] = {
};
but that doesnt seem to be working. I'm not sure how to solve this as I've never had to declare a pointer array like this before.
但这似乎不起作用。我不知道如何解决这个问题,因为我以前从来没有像这样声明过一个指针数组。
Can anyone help me please?
有人可以帮我吗?
Thanks
谢谢
采纳答案by Devolus
If you want an array of pointers to float, you must declare it as such. You declared just an array of floats. The name of the array is a pointer of course, but in the C sytnax it is treated the same and just a convenience.
如果你想让一个指针数组浮动,你必须这样声明它。您仅声明了一个浮点数组。数组的名称当然是一个指针,但在 C sytnax 中它被视为相同的,只是为了方便。
float *myIDs[] = {
//Variables
};
myIDs = new *float[n] = {
};
Alternatively you can use
或者你可以使用
float **myIDs;
myIDs = new **float;
And access it the same way like an array:
并以与数组相同的方式访问它:
float *x = myIDs[i];
浮动 *x = myIDs[i];
回答by zakinster
Note that you're not allocating an array of pointer but just an array of float, so basically you two array would have the same type, they just won't be stored in the same memory space.
请注意,您分配的不是指针数组,而是浮点数组,因此基本上您的两个数组将具有相同的类型,它们不会存储在相同的内存空间中。
Only a statically allocated array can be initialized this way, a dynamically allocated one cannot be initialized to anything other than zero.
只能以这种方式初始化静态分配的数组,动态分配的数组不能初始化为零以外的任何值。
myIDs = new float[]();
But if you know the elements to put in the array, you don't need to allocate it dynamically.
但是如果您知道要放入数组的元素,则不需要动态分配它。
If you want to allocate an array of pointer, you have to do this :
如果你想分配一个指针数组,你必须这样做:
float* myIDs[size]; // statically
float** myIDs = new float*[size]; // dynamically
But only the statically allocated one (the first one) can be initialized the way you describe and of course, it must be initialized with pointers.
但是只有静态分配的(第一个)可以按照您描述的方式进行初始化,当然,它必须使用指针进行初始化。
回答by Tomasz
If you want to declare array in a dynamic way, you can do it like this:
如果你想以动态方式声明数组,你可以这样做:
float *array = new float[size];
array[0] = first_value;
array[1] = second_value;
etc;
Just remember to free memory when you no longer need it (e.g. in a class destructor)
请记住在不再需要内存时释放内存(例如在类析构函数中)
delete [] array;
回答by Roee Gavirel
If you want a dynamically allocated array you should use the following format (what you did seems more like C# not C++)
如果你想要一个动态分配的数组,你应该使用以下格式(你所做的看起来更像是 C# 而不是 C++)
//The declaration of the object in the class
float *myIDs;
//The allocation it self (you must know which size you want to allocate at this point)
myIDs = new float[size];//bring change "size" to whatever you need is.
回答by Suvarna Pattayil
Consider the following snippet,
考虑以下片段,
#include<iostream>
#include<stdlib.h>
int main(void)
{
int a[] = {1,2};
a =new int[2];
delete(a);
return 0;
}
This gives an error error: incompatible types in assignment of ‘int*' to ‘int [2]'
.
I am statically creating an array of int . a
is a pointer(it is of type int[2]) but it can't be used to point to other dynamically allocated arrays because they return pointer of type int*
.
这给出了一个错误error: incompatible types in assignment of ‘int*' to ‘int [2]'
。我正在静态创建一个 int 数组。a
是一个指针(它的类型为int[2])但它不能用于指向其他动态分配的数组,因为它们返回类型为 的指针int*
。
If you want to create an array dynamically you have to assign its address to a float*
如果要动态创建数组,则必须将其地址分配给 float*
float * a = new float[10] ;
Refer thistoo.
也参考这个。