Multidimensional arrays

This is a discussion on Multidimensional arrays within the C++ Programming forums, part of the General Programming Boards category; int MyArray[4][12]; Is that a 12 x (int[4]) array or a 4 x (int[12]) array? If you type MyArray[1], do ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Multidimensional arrays

    int MyArray[4][12];

    Is that a 12 x (int[4]) array or a 4 x (int[12]) array?
    If you type MyArray[1], do you "access" the 5:th or the 13:th element?

    I've made some minor tests on this and it seems like that MyArray[X][Y] is the same as (MyArray[Y]) [X], (X nr of int[Y] arrays), but that seems weird to me, that the X and Y change place.

    What about MyArray[A][B][C][D]... ???

    This is really confusing, lol!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This will print out the adresses, and there are 1000 bytes between each. In the second set, the adresses will print out with 10 bytes between each.
    Code:
    int main()
    {
       char MyArray[5][100][10];
    
       cout << (int)MyArray[0] << endl;
       cout << (int)MyArray[1] << endl;
       cout << (int)MyArray[2] << endl;
       cout << (int)MyArray[3] << endl;
       cout << (int)MyArray[4] << endl;
    
       cout << (int)MyArray[0][0] << endl;
       cout << (int)MyArray[0][1] << endl;
       cout << (int)MyArray[0][2] << endl;
       cout << (int)MyArray[0][3] << endl;
       cout << (int)MyArray[0][4] << endl;
    }
    It seems like

    MyArray[X] == &MyArray[X][0][0]

    and

    MyArray[0][X] == &MyArray[0][X][0]

    (The compiler "fills up" with [0] after the array if the dimensions doesn't agree, correct?).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    It seems the compiler does assume 0 is the other dimention is ommited...but this makes sense as all that dimention acts as is an offset....so no offset presented; none applied!

    1240040
    1241040
    1242040
    1243040
    1244040

    1240040
    1240050
    1240060
    1240070
    1240080

    The standard says that int Array[5][6] is implemented like an array of 5( int Array[6]).......

    A good way to look at multidimentional arrays is how you have to implement them in your own containters.....Scott Meyers discribes the use of proxy classes to make this possible......and this is simply applying how the compiler implements arrays itself internally (unfortunately operator[][] doesnt exist for built in types or classes - its all arrays of arrays)

    Code:
    #include <iostream>
    
    template<class T,int alpha,int beta>
    class MyArray{
    	class MyProxy{
    		T m_t[beta];//array of data
    	public:		
    		T& operator[](int i){return m_t[i];}
    	} m_p[alpha];//array of proxies
    public:
    	
    	MyProxy& operator[](int i){return m_p[i];}	
    };
    
    int main(void){
    
    	const int alpha = 4,
    			  beta = 5;
       	MyArray<int,alpha,beta> Arr;
       
    	for(int i = 0; i < alpha;++i)
       		for(int j = 0; j < beta;++j)
    			Arr[i][j] = i + j;
    
    	for(int i = 0; i < alpha;++i)
       		for(int j = 0; j < beta;++j)
    			std::cout << Arr[i][j] << std::endl;
    
    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hmm, nice illustration Fordy. Something interesting to toy with...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 07:16 PM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 10:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 11:45 PM
  4. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 03:16 PM
  5. Adding multidimensional arrays
    By newbie2C++ in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2001, 04:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21