2d arrays quickie

This is a discussion on 2d arrays quickie within the C++ Programming forums, part of the General Programming Boards category; (checking myself here) when you declare a 2d array, array[x][y], is an array created that has x-1 elements 'along' and ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    48

    2d arrays quickie

    (checking myself here) when you declare a 2d array, array[x][y], is an array created that has x-1 elements 'along' and y 'down' or are both 0 based hence an array[3][2] will be accesible in the range array[0 thru 2][0 thru 1].

    thanks,
    astride a storied past, thats everywhere you are

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > or are both 0 based hence an array[3][2] will be accesible in the range array[0 thru 2][0 thru 1].
    Yes, like this

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    48
    crikey, you wasted no time thanks.
    astride a storied past, thats everywhere you are

  4. #4
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Arrow Suggestion...

    A '2d' Array can come in many shapes sizes and forms. That is one of the most ineficient ones...

    The 'best' way to do this is to use a pointer and a little math:

    blah *myarray = new blah[sizex*sizey];

    myarray[(y*sizex)+x] = wutever;

    Hope that helps .

    SPH

  5. #5
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Post Another thing...

    I also suggest instead of simple arrays using a linked list... but that is far more to talk about... if interested reply or email!

    SPH

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Another question, does array[x][y] create x arrays of y, or y arrays of x?

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >That is one of the most ineficient ones...

    Why? Dynamic allocation wasn't mentioned in the original or subsequent posts (until yours). Stack allocation will be quicker.

    > Another question, does array[x][y] create x arrays of y, or y arrays of x?

    x arrays of y.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > The 'best' way to do this is to use a pointer and a little math:
    Which is exactly what the compiler uses when you write [x][y] anyway, so what's your point?

  9. #9
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Exclamation lol....

    Once you've done a few projects... you will find the [][] sort of static pointer will not work as well as simply doing it yourself...

    Don't make bad habbits, this very thing almost wreked one of my projects:

    Try dynamic memory allocation here:

    char **blah = (char**)new char[x];
    *blah = new char[y];

    blah[1][1] = (char)6;

    you will usually need to set it up for a 'yx' call instead of 'xy', its jsut not a good habbit to 'form'.

    SPH

  10. #10
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >you will find the [][] sort of static pointer will not work as well as simply doing it yourself...

    Why?

    >Don't make bad habbits

    Thanks for the advice.

  11. #11
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    confused???

    howdy,
    i'm very confused???
    i am writung an app that will include 3 seperate arrays of about 200 objects each. which form do you suggest. pointers and math, linked list or dynamic memory allocation??
    the last 2 i have no idea how to do...oh well back to the books.

    M.R.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2d arrays
    By thamiz in forum C Programming
    Replies: 25
    Last Post: 05-25-2008, 05:06 AM
  2. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  3. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  4. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  5. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 06:33 AM

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