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

2d array

P: 79
i have several questions to ask about 2d array.

1. using c++/mfc, how can I get the size (width and height) of a 2d array with declaration like this:
int 2Darray[][]; ??

2. for java: short[][] rArray = new short[rowsize][heightsize]; how can I represent this sentence in c++ ??

3. how can i assign value to each in the 2d array?
Mar 22 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 1,510
i have several questions to ask about 2d array.

1. using c++/mfc, how can I get the size (width and height) of a 2d array with declaration like this:
int 2Darray[][]; ??

2. for java: short[][] rArray = new short[rowsize][heightsize]; how can I represent this sentence in c++ ??

3. how can i assign value to each in the 2d array?
have a look at multi-dimensional arrays in this tutorial
http://www.cplusplus.com/doc/tutorial/arrays.html
Mar 22 '07 #2

P: 79
have a look at multi-dimensional arrays in this tutorial
http://www.cplusplus.com/doc/tutorial/arrays.html
I've read that.

for q1, is there any method that can do like in java, 2Darray.length??
for q2, is this correct?
#############################
int **array;
array = new int [rowsize] [columnsize];
.................................
delete [ ][ ] array;
#############################
Mar 22 '07 #3

Expert 100+
P: 1,510
I would tend to
Expand|Select|Wrap|Line Numbers
  1.     float **imgdata;
  2.     imgdata=new float*[100];      // create float pointers
  3.     for(int i=0;i<100;i++)
  4.        {
  5.        imgdata[i]=new float[100]; // create floats
  6.        for(int k=0;k<100;k++)
  7.           imgdata[i][k]=k;        // assign values
  8.       }
  9.  
or
Expand|Select|Wrap|Line Numbers
  1.     float imgdata[100][100];
  2.     for(int i=0;i<100;i++)
  3.        {
  4.        for(int k=0;k<100;k++)
  5.           imgdata[i][k]=k;
  6.       }
Mar 22 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Here are your questions:

for q1, is there any method that can do like in java, 2Darray.length??
for q2, is this correct?
#############################
int **array;
array = new int [rowsize] [columnsize];
.................................
delete [ ][ ] array;
#############################

The answer to q1 is NO. Arrays in Java are objects and as objects they can have methods that could calculate the length. C++ avoids arrays and uses std::vector instead.

The answer to q2 is NO.

The "new" operator returns the address of the array. In C++ the address of an array is always the address of element 0. in your example, each element of the array is an array of int[columnsize]. You need a pointer that can contain the address of an array of int[columnsize]. This is the correct code:

int (*array)[columnsize] = new int[rowsize][columnsize];

This array has rowsize elements where each element is an int[columnsize].

Your delete is incorrect:

delete [ ][ ] array;


The correct delete is:

delete [] array;
Mar 26 '07 #5

Post your reply

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