Single dim array as a multidim array?

Discussion in 'C Programming' started by Sona, Sep 1, 2003.

  1. Sona

    Sona Guest

    Please could someone tell me how to use a single-dimensional array as a
    two-dimensional array? Is the following correct:

    int ROW_SIZE = 5;
    int COL_SIZE = 3;

    int *arr = (int*) malloc(sizeof(int) *ROW_SIZE*COL_SIZE);

    arr[i *COLUMN_SIZE + j];

    will this only work for square arrays or all two-dim arrays ? Thanks :)


    Sona
    Sona, Sep 1, 2003
    #1
    1. Advertising

  2. Sona <> wrote in
    <3f534c51$>:

    >Please could someone tell me how to use a single-dimensional array as a
    >two-dimensional array? Is the following correct:
    >
    >int ROW_SIZE = 5;
    >int COL_SIZE = 3;
    >
    >int *arr = (int*) malloc(sizeof(int) *ROW_SIZE*COL_SIZE);

    Nit1: arr isn't an array, albeit you can apply the [] operator
    to an pointer to int (which it is).

    Nit2: the cast to int* is superfluous in C.

    >
    >arr[i * COLUMN_SIZE + j];

    This will work as long as i is in [0 ; ROW_SIZE-1]
    and j is in [0 ; COL_SIZE-1].

    Much like in a RealArray(tm):

    int realArr[YDIM][XDIM];
    realArr[ y ][ x ] ...

    >
    >will this only work for square arrays or all two-dim arrays ? Thanks :)

    Well, for square arrays i and j are interchangeable, as long as
    you use them in a consistently, but it works for all n-dimensional
    arrays, e.g.:

    int *arr = (int*) malloc( ZDIM * YDIM * XDIM * sizeof *arr );

    arr[ z * XDIM * YDIM + y * XDIM + x ] = 42;

    Irrwahn

    --
    If it's not on fire, it's a software problem.
    Irrwahn Grausewitz, Sep 1, 2003
    #2
    1. Advertising

  3. Irrwahn Grausewitz <> wrote in
    <>:

    >Well, for square arrays i and j are interchangeable, as long as
    >you use them in a consistently, but it works for all n-dimensional

    ^^^^<del>
    >
    >int *arr = (int*) malloc( ZDIM * YDIM * XDIM * sizeof *arr );

    ^^^^^^
    Damn, now I've copied this stupid cast!

    --
    Learn how to splel, dmanit!
    Irrwahn Grausewitz, Sep 1, 2003
    #3
  4. Sona

    llewelly Guest

    Irrwahn Grausewitz <> writes:

    > Irrwahn Grausewitz <> wrote in
    > <>:
    >
    >>Well, for square arrays i and j are interchangeable, as long as
    >>you use them in a consistently, but it works for all n-dimensional

    > ^^^^<del>
    >>
    >>int *arr = (int*) malloc( ZDIM * YDIM * XDIM * sizeof *arr );

    > ^^^^^^
    > Damn, now I've copied this stupid cast!


    That's ok. Weirdos who compile your code on boxen where int and int*
    are not the same size deserve undefined behavior. Especially if
    you forgot to include a header. Remember, all the world is a
    wintel32.

    (Note to c.l.c++ readers: that cast isn't necessary or safe in C++
    either; the alternatives are:
    vector<int> arr(ZDIM * YDIM * XDIM);
    int *arr= new [ZDIM * YDIM * XDIM * sizeof *arr];
    int *arr= static_cast<int*> malloc( ZDIM * YDIM * XDIM * sizeof *arr );
    )
    llewelly, Sep 1, 2003
    #4
  5. Irrwahn Grausewitz wrote:

    > Sona <> wrote in
    > <3f534c51$>:
    >
    >
    >>Please could someone tell me how to use a single-dimensional array as a
    >>two-dimensional array? Is the following correct:
    >>
    >>int ROW_SIZE = 5;
    >>int COL_SIZE = 3;
    >>
    >>int *arr = (int*) malloc(sizeof(int) *ROW_SIZE*COL_SIZE);

    >
    > Nit1: arr isn't an array, albeit you can apply the [] operator
    > to an pointer to int (which it is).
    >
    > Nit2: the cast to int* is superfluous in C.
    >


    It's a bit worse than superfluous. It can be dangerous (as can any cast).

    It's necessary in C++, but in C++ you normally shouldn't use 'malloc' at
    all. In nearly all cases you should use 'new' instead.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Sep 1, 2003
    #5
  6. Kevin Goodsell <> wrote in
    <3f539667@shknews01>:
    >
    >It's necessary in C++, but in C++ you normally shouldn't use 'malloc' at
    >all. In nearly all cases you should use 'new' instead.
    >

    From the use of malloc() in the OP's example I derived that this was a
    C-question - though it was cross-posted to c.l.c and c.l.c++, like your
    reply, and mine too...

    --
    I wish life had a scroll-back buffer.
    Irrwahn Grausewitz, Sep 1, 2003
    #6
  7. Sona

    Al Bowers Guest

    Sona wrote:
    > Please could someone tell me how to use a single-dimensional array as a
    > two-dimensional array? Is the following correct:
    >
    > int ROW_SIZE = 5;
    > int COL_SIZE = 3;
    >
    > int *arr = (int*) malloc(sizeof(int) *ROW_SIZE*COL_SIZE);
    >
    > arr[i *COLUMN_SIZE + j];
    >
    > will this only work for square arrays or all two-dim arrays ? Thanks :)
    >


    I like:
    int i, **arr;
    arr = malloc(sizeof(*arr)*ROW_SIZE);
    for(i = 0;i < ROW_SIZE; i++)
    arr = malloc(sizeof(**arr)*COL_SIZE);
    arr[4][2] = 6; /* assuming successful allocations */

    This is only one of various ways you can do this. The faq
    demonstrates this technique and others at:
    http://www.eskimo.com/~scs/C-faq/q6.16.html

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x)
    http://www.geocities.com/abowers822/
    Al Bowers, Sep 1, 2003
    #7
  8. Sona

    foo Guest

    llewelly <> wrote in message news:<>...
    > Irrwahn Grausewitz <> writes:
    >
    > > Irrwahn Grausewitz <> wrote in
    > > <>:
    > >
    > >>Well, for square arrays i and j are interchangeable, as long as
    > >>you use them in a consistently, but it works for all n-dimensional

    > ^^^^<del>
    > >>
    > >>int *arr = (int*) malloc( ZDIM * YDIM * XDIM * sizeof *arr );

    > > ^^^^^^
    > > Damn, now I've copied this stupid cast!

    >
    > That's ok. Weirdos who compile your code on boxen where int and int*
    > are not the same size deserve undefined behavior. Especially if
    > you forgot to include a header. Remember, all the world is a
    > wintel32.
    >
    > (Note to c.l.c++ readers: that cast isn't necessary or safe in C++
    > either; the alternatives are:
    > vector<int> arr(ZDIM * YDIM * XDIM);
    > int *arr= new [ZDIM * YDIM * XDIM * sizeof *arr];
    > int *arr= static_cast<int*> malloc( ZDIM * YDIM * XDIM * sizeof *arr );
    > )

    IMHO, a better C++ alterantive would be to use either a
    vector<vector<int> > object or even better yet, the following dynamic
    2 dimensional array object:
    template < class T, int ROW_T = 0, int COL_T = 0 >
    class dynamic_2d_array
    {
    public:
    dynamic_2d_array(int row, int col):m_row(row),m_col(col),
    m_data((row!=0&&col!=0)?new T[row*col]:NULL){}
    dynamic_2d_array():m_row(ROW_T),m_col(COL_T), m_data(new
    T[ROW_T*COL_T])
    {if (!COL_T || !ROW_T) {int x[ROW_T] = {{ROW_T}};int y[COL_T] =
    {{x[0]}};}}
    ~dynamic_2d_array(){if(m_data) delete []m_data;}
    T* operator[](int i) {return (m_data + (m_col*i));}
    T const*const operator[](int i) const {return (m_data +
    (m_col*i));}
    private:
    const int m_row;
    const int m_col;
    T* m_data;
    };
    See following link for usuage example and for vector<vector<type> >
    example:

    http://axter.com/faq/topic.asp?TOPIC_ID=60&FORUM_ID=4&CAT_ID=9
    foo, Sep 1, 2003
    #8
  9. (foo) wrote in
    <>
    a lot of semi-cryptical C++ code. Please do not cross-post
    C++ stuff to c.l.c.
    --
    When things look dark,
    hold your head high so it can rain up your nose.
    Irrwahn Grausewitz, Sep 1, 2003
    #9
  10. Sona

    foo Guest

    Irrwahn Grausewitz <> wrote in message news:<>...
    > (foo) wrote in
    > <>
    > a lot of semi-cryptical C++ code. Please do not cross-post
    > C++ stuff to c.l.c.
    >>>>Nit2: the cast to int* is superfluous in C.



    And please do not cross-post your superfluous C remarks to c.l.c++

    Thank you very much :)
    foo, Sep 2, 2003
    #10
  11. Sona

    LibraryUser Guest

    foo wrote:
    > Irrwahn Grausewitz <> wrote in message
    > > (foo) wrote in
    > >
    > > a lot of semi-cryptical C++ code. Please do not cross-post
    > > C++ stuff to c.l.c.


    > >>>>Nit2: the cast to int* is superfluous in C.

    >
    > And please do not cross-post your superfluous C remarks to c.l.c++


    You are both at fault here, not to mention the OP (who should not
    have crossposted in the first place). Both of you should have
    set followups. This is the appropriate mechanism to advise
    across newsgroups, and prevent further confusion.

    --
    Replies should be to the newsgroup
    Chuck Falconer, on vacation.
    LibraryUser, Sep 2, 2003
    #11
    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. Sona
    Replies:
    10
    Views:
    571
    LibraryUser
    Sep 2, 2003
  2. Levent
    Replies:
    8
    Views:
    377
    baumann@pan
    May 17, 2005
  3. Salman Khilji

    MultiDim Array Challenge

    Salman Khilji, Feb 28, 2004, in forum: C Programming
    Replies:
    2
    Views:
    400
    Barry Schwarz
    Feb 28, 2004
  4. Denis Palas
    Replies:
    1
    Views:
    501
  5. Florian Kaufmann
    Replies:
    1
    Views:
    377
Loading...

Share This Page