Two Dimensional Arrays

Discussion in 'C++' started by cplusplusquestion@gmail.com, Apr 11, 2008.

  1. Guest

    There is a two-dimensional array:

    int grades[MAX][MAX];
    for( i = 0; i < MAX; i++)
    for( j = 0; j < MAX; j++)
    grades[j] = -1;


    I would like to assign an another variable to this array, for example:

    int another_grades[MAX][MAX];
    for( i = 0; i < MAX; i++)
    for( j = 0; j < MAX; j++)
    another_grades[j] = grades[j];

    Here, I need to declare another array. Is it possible to have a
    pointer to point grades[MAX][MAX]? As I've tried:

    int** another_g = grades;

    it does not work. Any good idea?
    , Apr 11, 2008
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > There is a two-dimensional array:
    >

    Please don't multi-post on Usenet, answered elsewhere.

    --
    Ian Collins.
    Ian Collins, Apr 11, 2008
    #2
    1. Advertising

  3. Tim Love Guest

    writes:

    >Here, I need to declare another array. Is it possible to have a
    >pointer to point grades[MAX][MAX]? As I've tried:


    >int** another_g = grades;


    >it does not work. Any good idea?


    The trouble is that unless the compiler knows the shape of the matrix that
    another_g points to, it won't be able to work out where (e.g.) element
    another_g[3][3] is in memory.

    Have a look at
    http://burks.bton.ac.uk/burks/language/c/ptrtut/ch7x.htm
    Tim Love, Apr 11, 2008
    #3
  4. Guest

    On 11 Apr., 07:37, wrote:
    > There is a two-dimensional array:
    >
    > int grades[MAX][MAX];
    > for( i = 0; i < MAX; i++)
    > for( j = 0; j < MAX; j++)
    > grades[j] = -1;
    >
    > I would like to assign an another variable to this array, for example:
    >
    > int another_grades[MAX][MAX];
    > for( i = 0; i < MAX; i++)
    > for( j = 0; j < MAX; j++)
    > another_grades[j] = grades[j];
    >
    > Here, I need to declare another array. Is it possible to have a
    > pointer to point grades[MAX][MAX]? As I've tried:
    >
    > int** another_g = grades;
    >
    > it does not work. Any good idea?


    While native arrays are perfectly legal C++, I recommend to use one of
    the standard classes std::vector<> or std::valarray<> instead. There
    is no performance penalty in using them and only a very small increase
    in memory footprint. The benefits by far outweigh the tiny overhead.

    I would change your code like this:

    #include <vector>
    ....
    std::vector< std::vector<int> > grades( MAX, std::vector<int>( MAX,
    -1 ) );
    // initializes all elements to -1, no explicit loops!

    std::vector< std::vector<int> > another_grades1 = grades;
    // copy the whole 2D array to another location.

    std::vector< std::vector<int> >& another_grades2 = grades;
    // create a reference (implicit pointer) variable to the same
    location.

    std::vector< std::vector<int> >* another_grades3 = &grades;
    // create an explicit pointer to the 2D array.

    Access to the std::vector elements looks the same as with native
    arrays:

    grades[5][3] = 1;
    another_grades1[3][5] = 2;
    another_grades2[1][1] = 3;
    (*another_grades3)[0][0] = 4;

    std::valarray<> is an alternative with access methods optimized for
    linear algebra at the expense of a less intuitive interface.

    best,

    Michael
    , Apr 11, 2008
    #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. Alf P. Steinbach
    Replies:
    0
    Views:
    421
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,911
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    638
    Vivek
    Aug 21, 2003
  4. Kev Jackson
    Replies:
    2
    Views:
    99
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    194
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page