Hello:
Am getting confused about making a 2 dimensional array of pointers allocate storage space dynamically in C++. Actually I just want the second dimension of the array to be dynamic in size. For e.g. a simpler version of what I want to do would be:
Instead of,

int p[10];

I would do

int *p;
int x=10; // OR available thru user input

p = new int [x];

but what I have currently is:

int *p[2][5]; // array of 2x5 pointers [\code]

and want to convert to allow the user to enter the size of second dimension (which is [5] right now). Maybe the declaration will look like:

[code=c] (int *) (* p[2]); [\code]

Not sure how to go around allocating space for the second dimension of the array?

Any help would be appreciated.

Thanks.[code=c]
int *p[2][5]; // array of 2x5 pointers
[\code]

and want to convert to allow the user to enter the size of second dimension (which is [5] right now). Maybe the declaration will look like:

(int *) (* p[2]); [\code]

Not sure how to go around allocating space for the second dimension of the array?

Any help would be appreciated.

Thanks.[code=c]
(int *) (* p[2]);
[\code]

Not sure how to go around allocating space for the second dimension of the array?

Any help would be appreciated.

Thanks.

I'm not sure but could you do something like this?

int nSize=5;
int *2dArray = [2][nSize];
cin >> nSize;

maybe someone more experienced than me could help more because , like I said. I think it will work but I'm not sure.

Here is a general purpose set up that should work for you.

int rows, cols;
// get rows and cols from user
int ** array2d;
array2d = new int*[rows];
for (int i = 0; i < rows; i++)
    array2d[i] = new int[cols];

Thanks.. I tried a few different things which got complicated, ended up using a "map" instead which would look something like:

typedef std::pair<UINT8, UINT16> tABC;
std::map<tABC, int*> xyz;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.