指向固定大小数组的指针数组 [英] Array of pointers to arrays of Fixed Size

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

问题描述



我需要一些帮助来声明指向某个固定大小的数组的指针数组。我希望指针指向仅固定

大小的数组(不适用于相同类型的可变大小数组)。


例如:

int arr1 [20]; // 20个整数的数组

int arr2 [20];

int arr3 [20];

............

int arrn [20];

int * arrofptr [10]; //数组10指向int的指针

arrofptr [0] = arr1;

arrofptr [1] = arr2;

等等...

但在这种情况下,用户也可以将其指向不同的b / b
大小的数组...说

int arr4 [30]; //大小30

arrofptr [3] = arr4;

所以有没有办法声明指针数组,以便它们指向数组
到数组只有一个固定大小,没有任何运行时检查


阵列指向的大小?

-Linvin

Hi,
I need some help in declaring an array of pointers to array of a
certain fixed size. I want the pointers to point to arrays of fixed
size only (should not work for variable sized arrays of the same type).

eg:
int arr1[20];//Array of 20 ints
int arr2[20];
int arr3[20];
............
int arrn[20];
int *arrofptr[10];//Array of 10 pointers to int
arrofptr[0] = arr1;
arrofptr[1] = arr2;
and so on...
But in this case a user can also point it to an array of a different
size... say
int arr4[30];// Size 30
arrofptr[3]=arr4;
So is there a way to declare the array of pointers so that they point
to arrays of a FIXED size only,without any runtime check on the size of

arrays being pointed to ?
-Linvin

推荐答案




Linny写道:


Linny wrote:

我需要一些帮助declari指向某个固定大小的数组的指针数组。我希望指针只指向固定大小的数组(不适用于相同类型的可变大小数组)。

例如:
int arr1 [20]; // 20个整数的数组
int arr2 [20];
int arr3 [20];
...........
int arrn [20] ;

int * arrofptr [10]; //指向int的10个指针的数组

arrofptr [0] = arr1;
arrofptr [1] = arr2;

等等......
但在这种情况下,用户也可以将其指向不同大小的数组...说
int arr4 [ 30]; //大小30
arrofptr [3] = arr4;

所以有没有办法声明指针数组,以便它们指向一个FIXED的数组只有大小,没有任何运行时检查指向的数组的大小?
Hi,
I need some help in declaring an array of pointers to array of a
certain fixed size. I want the pointers to point to arrays of fixed
size only (should not work for variable sized arrays of the same type).

eg:
int arr1[20];//Array of 20 ints
int arr2[20];
int arr3[20];
...........
int arrn[20];
int *arrofptr[10];//Array of 10 pointers to int
arrofptr[0] = arr1;
arrofptr[1] = arr2;
and so on...
But in this case a user can also point it to an array of a different
size... say
int arr4[30];// Size 30
arrofptr[3]=arr4;
So is there a way to declare the array of pointers so that they point
to arrays of a FIXED size only,without any runtime check on the size of
arrays being pointed to ?




容易不给用户选择填写数组的大小。


如果你的意思是另一个程序员而不是用户请记住

另一个程序员可以忽略你想做的任何事情无论是什么

(s)他想要的。

那说你可以发表评论说明阵列必须是某个固定大小的b / b
然后使用断言/异常强制执行

大小。


哦,请不要使用像这样的魔法数字来养成

执行以下操作:

const unsigned int ARRAYSIZE = 20;

int arr1 [ARRAYSIZE];

int arr2 [ARRAYSIZE];

.....

int arrn [ARRAYSIZE];



Easy don''t give the user an option to fill in the size of the array.

If you mean another programmer instead of user keep in mind that
another programmer can just ignore anything you want and do whatever
(s)he wants.
That said you can put in comments stating that the arrays have to be a
certain fixed size and then using asserts / exceptions to enforce that
size.

Oh and please do not use magic numbers like that get in a habit of
doing the following:
const unsigned int ARRAYSIZE = 20;
int arr1[ARRAYSIZE];
int arr2[ARRAYSIZE];
.....
int arrn[ARRAYSIZE];


" Linny" <李******* @ gmail.com> schrieb im Newsbeitrag

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
"Linny" <li*******@gmail.com> schrieb im Newsbeitrag
news:11**********************@f14g2000cwb.googlegr oups.com...

我需要一些帮助来声明指向某个固定大小数组的指针数组。我希望指针只指向固定大小的数组(不适用于相同类型的可变大小数组)。

例如:
int arr1 [20]; // 20个整数的数组
int arr2 [20];
int arr3 [20];
...........
int arrn [20] ;

int * arrofptr [10]; //指向int的10个指针的数组

arrofptr [0] = arr1;
arrofptr [1] = arr2;

等等......
但在这种情况下,用户也可以将其指向不同大小的数组...说
int arr4 [ 30]; //大小30
arrofptr [3] = arr4;

所以有没有办法声明指针数组,以便它们指向一个FIXED的数组只有大小,没有任何运行时检查

数组的大小指向?

-Linvin
Hi,
I need some help in declaring an array of pointers to array of a
certain fixed size. I want the pointers to point to arrays of fixed
size only (should not work for variable sized arrays of the same type).

eg:
int arr1[20];//Array of 20 ints
int arr2[20];
int arr3[20];
...........
int arrn[20];
int *arrofptr[10];//Array of 10 pointers to int
arrofptr[0] = arr1;
arrofptr[1] = arr2;
and so on...
But in this case a user can also point it to an array of a different
size... say
int arr4[30];// Size 30
arrofptr[3]=arr4;
So is there a way to declare the array of pointers so that they point
to arrays of a FIXED size only,without any runtime check on the size of

arrays being pointed to ?
-Linvin




AFAIK是不可能的。

尝试一个workarround。例如,您可以使用固定的

数组创建一个结构:

const unsigned int C_uiFixedSize = 20;


struct FixedArray

{

int arr [20];

};


int main()

{

FixedArray arr1;

FixedArray * arrofptr [10];

arrofptr [0 ] =& arr1;

}


Greets Chris



AFAIK it is not possible.
Try a workarround. For example you could create a struct with your fixed
array in it:

const unsigned int C_uiFixedSize = 20;

struct FixedArray
{
int arr[20];
};

int main()
{
FixedArray arr1;
FixedArray *arrofptr[10];
arrofptr[0] = &arr1;
}

Greets Chris


可能你可以使用上课.....


class my_array {


public:

int arr1 [20] ; // 20个整数的数组

int arr2 [20];

int arr3 [20];

....... ....

int arrn [20];


int * arrofptr [10]; //指向int的10个指针的数组


arrofptr [0] = arr1;

arrofptr [1] = arr2;

.............


//定义复制自身复制构造函数的方法

int * operator =(int *){

//地方执行ch的方法eck用户没有尝试

来破坏逻辑

}

}


在此如果您不允许,则用户将无法将阵列复制到不同大小。

May be you can use a class for that.....

class my_array {

public:
int arr1[20];//Array of 20 ints
int arr2[20];
int arr3[20];
...........
int arrn[20];

int *arrofptr[10];//Array of 10 pointers to int

arrofptr[0] = arr1;
arrofptr[1] = arr2;
.............

//define the method for copy self copy constructor
int * operator =(int *) {
//place the method to perform the check that user is not trying
to disrupt the logic
}
}

In this case if you do not permit then user will be not able to copy
the array to different size.


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

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