Advertisement

array dimensions

Started by November 09, 2000 04:38 PM
6 comments, last by a38750 24 years, 2 months ago
I''m sure I read somewhere that you can do this:

int m[4][4];
for(int i=0;i<16;i++) m=i;
I tried it and it comes up with wierd errors. Can you do this, or do I have to do 2 for loops? Cheers, Frank
AFAIK you need to do 2 loops, one nested in the other, to walk the dimensions properly.

--HB

Advertisement
Your could try with

  int m[4][4];int*m_ptr=(int*)mfor(int i=0;i<16;i++) m_ptr++=i;  


Then use m as normal.
never mind. I read that it''d work in C++ for Dummies or something so no wonder!
afraid its a bit more complicate
  int** mAs2DArray;int* mAs1DArray;//allocate memeory as a 1D arraymAs1DArray = new int[16];//allocate pointer memory for 2D arraymAs2DArray = new int*[4];//assign 2D array to 1D array positionsfor(int j=0;j<4;j++)	mAs1DArray[j] = mAs1DArray[j*4];//know you can use mAs1DArray[0..15]//and mAs2DArray[0..3][0..3]//and the reference the same memory locations//clean up//Don''t do this, 1D array will clean this memory up//for(int j=0;j<4;j++)//	delete[] mAs2DArray[j];delete[] mAs2DArray;delete[] mAs1DArray;  
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Magmai
Why are you allocating memory for two arrays? And where do you put the values in mAs2DArray?

My code should work, I am basically converting the 2D array two a 1D one. If its clearear you could use m_ptr instead of m_ptr++.
Advertisement
I ment
    m_ptr<i>;  


Edited by - fredizzimo on November 9, 2000 7:28:49 PM
Why doesn''t the index show up correctly?

This topic is closed to new replies.

Advertisement