2D array of structures

Discussion in 'C++' started by michi, Nov 27, 2004.

  1. michi

    michi Guest

    Hello,

    I need to initialize a 2 dimensional square arrays of structures. The size of array I get from the user. I can
    do one-dimensional array, but I don't know how to specify the size of array when I want to do it 2D. I want
    to do it using 'new' operator.

    Thanks, michi.
    michi, Nov 27, 2004
    #1
    1. Advertising

  2. "michi" <> wrote...
    > I need to initialize a 2 dimensional square arrays of structures. The size
    > of array I get from the user. I can
    > do one-dimensional array, but I don't know how to specify the size of
    > array when I want to do it 2D. I want
    > to do it using 'new' operator.


    This is covered in the FAQ. http://www.parashift.com/c -faq-lite/
    Victor Bazarov, Nov 28, 2004
    #2
    1. Advertising

  3. michi

    someone else Guest

    using a simple google groups search ((c++ 2d array new), i found this
    i have tested it, and it works like a charm.

    (...)
    If you need a 2D
    array with both (or the second) dimension variable, you will have to
    do it by hand, either by using an array of pointers, and allocating
    each of the sub-arrays separately, or by declaring and allocating a 1D
    array, and doing the index calculation by hand. (I generally choose
    the second solution for quick and dirty software, encapsulated in a
    class, of course.)

    So, in the above case, I might write something like the following:

    int n = 4 ;
    int m = 5 ;

    int* a = new int[ m * n ] ;
    a[ m * i + j ] ; // e.g.: a[ i ][ j ].

    A bit messy, but once it's hidden in a class, it's quite presentable.
    -----------------
    #include <iostream.h>

    template< class T >
    class Array2D
    {
    public :
    Array2D( int n , int m ) ;
    ~Array2D() ;
    T* operator[]( int j ) ;
    private :
    int dim2 ;
    T* a ;
    } ;

    template< class T >
    Array2D< T >::Array2D( int n , int m )
    : dim2( n )
    , a( new T[ n * m ] )
    {
    }

    template< class T >
    Array2D< T >::~Array2D()
    {
    delete [] a ;
    }

    template< class T >
    T*
    Array2D< T >::eek:perator[]( int j )
    {
    return &a[ dim2 * j ] ;
    }


    int
    main()
    {
    Array2D< int > a( 4 , 5 ) ;
    for ( int i = 0 ; i < 4 ; i ++ )
    {
    for ( int j = 0 ; j < 5 ; j ++ )
    {
    a[ i ][ j ] = 10 * i + j ;
    cout << a[ i ][ j ] << endl ;
    }
    } return 0 ;
    }
    --------------
    Obviously, I've cheated with regards to the copy constructor and the
    assignment operator. Either declare them private, or provide real
    ones.

    The only tricky part is the return value of operator[], and even that
    isn't too tricky, if you remember the relationship between arrays and
    pointers. (Operator[] is used in normal expressions, the one place
    where arrays and pointers *are* sort of the same thing.) Basically,
    although the class allocates the array as a 1D array, it considers it
    logically as a 2D array, doing index calculation as necessary. As we
    said, C++ doesn't have 2D arrays, but it has arrays of arrays. So
    operator[] pretends that its internal array (pointed to by a) is an
    array[] of array[dim2] of T, and calculates the address of the
    array[dim2] selected by the index. In theory, it would return the
    selected array (or a reference to it); in practice, an array appearing
    in an expression converts automatically to the address of its first
    element, so operator[] returns the address of the first element of the
    sub-array. Conveniently, this is a valid type of the (built-in)
    operator[], which handles the "second" dimension. (Operator[] could
    just as easily return a reference to an array, but this would take
    some casting in the operator[] itself, and there is such a long
    tradition for doing it this way.)
    --
    James Kanze email:
    GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
    Conseils en informatique industrielle --

    "michi" <> wrote in message
    news:Xns95AF17935411michifossilgroupnet@195.34.132.65...
    > Hello,
    >
    > I need to initialize a 2 dimensional square arrays of structures. The size
    > of array I get from the user. I can
    > do one-dimensional array, but I don't know how to specify the size of
    > array when I want to do it 2D. I want
    > to do it using 'new' operator.
    >
    > Thanks, michi.

    http://groups.google.com.au/groups?-es.sel.de&rnum=1
    someone else, Nov 28, 2004
    #3
  4. michi wrote:
    > Hello,
    >
    > I need to initialize a 2 dimensional square arrays of structures. The size of array I get from the user. I can
    > do one-dimensional array, but I don't know how to specify the size of array when I want to do it 2D. I want
    > to do it using 'new' operator.
    >
    > Thanks, michi.


    see:
    http://groups.google.com/groups?hl=en&lr=&selm=
    Gianni Mariani, Nov 28, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. michi

    Array of structures

    michi, Nov 18, 2004, in forum: C++
    Replies:
    8
    Views:
    506
    Andrey Tarasevich
    Nov 19, 2004
  2. tweak
    Replies:
    14
    Views:
    2,760
    Eric Sosman
    Jun 11, 2004
  3. Alfonso Morra
    Replies:
    11
    Views:
    701
    Emmanuel Delahaye
    Sep 24, 2005
  4. valerio
    Replies:
    3
    Views:
    361
  5. Amit  Limaye
    Replies:
    4
    Views:
    443
    Phlip
    Apr 10, 2006
Loading...

Share This Page