Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

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.

Example.

int **ptr;

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

Please I need the answer not work arounds or alternatives
Posted
Updated 22-Aug-21 5:51am
v4

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:
C++
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);
	}
}
 
Share this answer
 
v3

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.

 
Share this answer
 
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):
C++
// 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);
};

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900