指向多维数组的指针 [英] Pointers to multiple dimension arrays

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

问题描述

我有2个数组,分别是int a [10] [20]和int b [10] [20].我想为一个或另一个数组创建一个点并读取数据.在一种情况下,我可能需要数据,而在另一种情况下,我可能需要b数据.

I have 2 arrays, int a[10][20] and int b[10][20]. I want to create a point to one or the other array and read the data. In one condition I may want a data and the other I might want b data.

示例.

int ** ptr;

int **ptr;

ptr =& a;  //不起作用,无法找出如何为现有二维数组分配指针

ptr=&a;  // doesn''t work can''t figure out how to assign a pointer to and existing 2 dimension array

推荐答案

在C ++中指向多维​​数组的指针很棘手.通常,您必须指定每个维度的实际容量,这意味着一个指针不能用于两个不同大小的数组.如果您真的想要一个指针,我建议您制作一个单一维度的数组,然后就可以计算索引以使用多个维度.会像这样:
Pointers to multidimensional arrays in C++ is tricky business. Most often, you have to specify the actual capacity of each dimension, which means one pointer cannot be used for two different sized arrays. If you really want a pointer, I suggest making a single dimension array and then you can just calculate the index to use multiple dimensions. Would go something like this:
int wide = 640; int tall = 480;
int * pArray = new int[wide * tall];
int x; int y; int yOffset; int offset;
for(y = 0; y < tall; y++)
{
	yOffset = y * wide;
	for(x = 0; x < wide; x++)
	{
		offset = yOffset + x;
		// Use pointer arithmetic to assign element at (x,y).
		*(pArray + offset) = CalculateSomething(x, y);
	}
}


好吧,它们要么是相同的数组,要么是两个不同的数组.您可以编写一个类来交换它查找的指针,但是不能有两个数组也都是相同的数组,但有时是这样.

Well, they are either the same array, or two different arrays.  You could write a class that swaps which pointer it looks up, but you can''t have two arrays that are also the same array, but only sometimes.


好吧,我尝试过,但是我完全困惑为什么它不起作用(请注意奇怪的错误,在其所在行上方的注释中):
Well, I tried, but I''m completely baffled as to why this doesn''t work (note the strange error, which is in a comment above the line it occurs at):
// Interface to access array functions (get value, set value).
template<class T>
class IArrayHolder{
public:
	virtual T GetVal(int x, int y) = 0;
	//...Set value...
};

// Class to wrap an array.
template<class T, class S>
class ArrayHolder: public IArrayHolder<S>
{
private:
	T multiArray;
public:
	ArrayHolder(T val){
		// error C2440: '=' : cannot convert from 'int [][20]' to 'int [][20]'
		multiArray = val;
	}
	S GetVal(int x, int y){
		return multiArray[x][y];
	};
};

// Some function that uses multiple multi-dimension arrays.
System::Void SomeFunction(){
	int x[10][20];
	int y[20][30];
	IArrayHolder<int> * xInterface;
	xInterface = new ArrayHolder<int[][20], int>(x);
	IArrayHolder<int> * yInterface;
	yInterface = new ArrayHolder<int[][30], int>(y);
};

// Some function that accepts a 2D array.
System::Void AnotherFunction(IArrayHolder<int> * arrayHolder){
	int val = arrayHolder->GetVal(20, 20);
};


您找出该错误,然后就可以解决了.如果不是这样,也许您应该只将数组值复制到2D向量中,然后使用它.另外,我对void指针没有太多的经验,但是它们显然可以指向任何对象的地址,因此您可以尝试使用它们.


You figure out that error, and you''ve go your solution. If not, perhaps you should just copy the array values to a 2D vector and use that instead. Also, I don''t have much experience with void pointers, but they can apparently point to the address of anything, so you might try working with those.


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

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