445,819 Members | 1,173 Online
+ 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
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     float **imgdata;     imgdata=new float*[100];      // create float pointers     for(int i=0;i<100;i++)        {        imgdata[i]=new float[100]; // create floats        for(int k=0;k<100;k++)           imgdata[i][k]=k;        // assign values       }   or Expand|Select|Wrap|Line Numbers     float imgdata[100][100];     for(int i=0;i<100;i++)        {        for(int k=0;k<100;k++)           imgdata[i][k]=k;       } Mar 22 '07 #4

 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