0

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.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by amare_de
0

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.

1

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];
0

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;

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.