By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,907 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,907 IT Pros & Developers. It's quick & easy.

arrays

P: 1
i want to declare array size dynamically for double dimensional.
int a [][];
May 28 '07 #1
Share this Question
Share on Google+
3 Replies


gpraghuram
Expert 100+
P: 1,275
HI,
Then u should use a pointer like this
int **ptr; and allocate memory for each dimension dynamically.

Thanks
Raghuram
May 28 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This is not correct.

Then u should use a pointer like this
int **ptr; and allocate memory for each dimension dynamically
An int** ptr is not a pointer to a 2D array. All it is is a pointer to an int pointer to a single int.

A 2D array like this:
Expand|Select|Wrap|Line Numbers
  1. int arr[10][3];
  2.  
is an array of 10 elements where each element is an array of 3 int. Now, in C++ the name of the array is the address of element 0. Therefore the name arr is the address of an array of 3 int. That is, arr is a pointer to an array of three int.

So, when you dynamically allocate a 2D array, new will return the address of element 0. This will be the address of an array of 3 int. You need to declare a pointer to an array of 3 int to receive the address returned by new:

Expand|Select|Wrap|Line Numbers
  1. int (*arr)[3] = new int[10][3];
  2.  
May 28 '07 #3

P: 93
i want to declare array size dynamically for double dimensional.
int a [][];
The best thing is create an object of Standard Templete Library class Vector
Which will grow dynamically when you insert new elements into it.
It will even shrink when you remove some elements from this vector.

Hope this may help.
May 29 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.