Arrays

This is a discussion on Arrays within the C++ Programming forums, part of the General Programming Boards category; arrays in c are so more complicated than java, i wonder if someone could help how do i create an ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    28

    Arrays

    arrays in c are so more complicated than java, i wonder if someone could help

    how do i create an array of array of RECTS like below

    e.g.

    Code:
    array
    |                 |
    |                 |
    |                 |
    array             array
     |      |         |      |
     |      |         |      |
     |      |         |      |
     |      |         |      |
    rect  rect       rect  rect
    any help much appreciated

    thanks

    a1dutch

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    RECT** array_to_array;
    RECT array_to_array2[1][1]; // The first index is the first dimension and so on..
    You could also write a function to allocate memory for you:
    Code:
    RECT** RectMemAlloc(int x, int y) {
        RECT** temp = new RECT*[x];
        for(int i = 0; i < x; i++)
            temp[i] = new RECT[y];
        return temp;
    }
    
    void RectFreeMem(RECT** free_me, int x) {
        for(int i = 0; i < x; i++) {
            delete[] free_me[i];
            free_me[i] = 0;
        }
        delete[] free_me;
        free_me = 0;
    }
    
    int main() {
        RECT** alloc_me = RectMemAlloc(4, 4);
        RectFreeMem(alloc_me, 4);
    }
    This should work but I haven't tested it and it's off the top of my head so there might be something wrong but it's something like this.
    Last edited by Desolation; 05-15-2006 at 11:49 AM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >arrays in c are so more complicated than java
    Only because you're not familiar with them. Arrays in C are lower level, and conceptually much simpler than Java arrays.

    >how do i create an array of array of RECTS
    RECT array[2][2];
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    now im confused with two answers that have been given. what im doing is writing a basic computer game so freeing up memory is not important. i have 32 rectangles arrays which each can have further arrays of RECTS say a maximum of 5.

    is the code below right to create these?

    Code:
           RECT array[32][5];
    i will know the numbers of the arrays to access (say i need array 5) so to access them do i use?

    Code:
    RECT tmp[5];
    
    for (int i =0; i !=5; i++)
    {
        tmp[i] = array[5][i];
    }
    Last edited by a1dutch; 05-15-2006 at 12:23 PM.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    The first code is correct. However, to get each RECT, you first have to go through all the first dimension RECTS which will give you RECT*'s or RECT[5]'s if you prefer. Then, you can access the RECTs as you are doing it right now.

    Nevermind, your edit is correct; however, you have to change != to < because otherwise you will get 6 indices (0,1,2,3,4,5) instead of 5 (0,1,2,3,4).

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    sorry im maybe not explaining propperly, il explain again.

    i need to pass an array of RECT's to a function so that each can be checked


    Code:
    //create array
    RECT array[32][5];
    
    //values will be stored statically
    
    //.....  more program code
    
    
    //get an array of rects to pass to another function
    RECT tmp[5];
    
    for (int i =0; i !=5; i++)
    {
        tmp[i] = array[5][i];
    }
    
    
    //pass tmp array to function
    
    //inside receiving function
    tmp[5]; // tmp is the array of rects received
    for (int i = 0; i != 5; i++)
    {
        RECT t = tmp[i];
    }
    does that look right

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    I edited my post after you edited yours ;-)

    Edit: And yes it looks right, if you take in consideration the modification I told you to make in my previous post.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    cheers man ur a star

    and by the way != 5 is exactly the same as < 6

    i always prefer using != to <

    thanks again

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    one more question if i could, not on the subject of arrays but RECTs.

    if a RECT is no initialised are the values 0 as default?

    is it possible to store ?
    array[32][3] = null;
    array[32][4] = null;
    array[32][5] = null;

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    Dude, what I meant is you need "< 5" instead of "!= 5". If you really want to stick with != use "!= 4". As for your second question, the values are undetermined, if I recall. I don't think there's a default constructor therefore no specific value is given. It will be whatever crap was there before it was allocated so it could be -1 just as well as 234567 if you get what I mean. And for your last question, you cannot assign a RECT with a value. You can only assign values to its members.

    Oh, and by the way, in C/C++ it's NULL and not null like in Java.

    Last remark: remember that in C++ (probably in Java too) arrays start at index 0 and not 1 . This could cause you to bang your head on your home desktop

  11. #11
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by a1dutch
    cheers man ur a star

    and by the way != 5 is exactly the same as < 6

    i always prefer using != to <

    thanks again
    In general use < or <= for loop conditions.
    If you use != and the number you are checking against (say 5) is skipped for whatever reason (not likely to happen here but fairly easy in more complicated loops with non regular steps) the loop will not end. On the other hand if you use < or <= it will stop.

    Plus it is the stnadard practice and it will stop people from telling you to change it every time.

    Quote Originally Posted by Desolation
    Dude, what I meant is you need "< 5" instead of "!= 5". If you really want to stick with != use "!= 4". As for your second question, the values are undetermined, if I recall. I don't think there's a default constructor therefore no specific value is given. It will be whatever crap was there before it was allocated so it could be -1 just as well as 234567 if you get what I mean. And for your last question, you cannot assign a RECT with a value. You can only assign values to its members.

    Oh, and by the way, in C/C++ it's NULL and not null like in Java.

    Last remark: remember that in C++ (probably in Java too) arrays start at index 0 and not 1 . This could cause you to bang your head on your home desktop
    Uh?

    If the index variable (i) is lower than 5, i != 5 is equivalent to i < 5. If you use i != 4 it will stop one iteration too early.

    And by the way in c++ it's not null, or NULL it's 0. NULL is defined in some of the c headers but it is not (if I recall correctly) part of the c++ standard.

    one more question if i could, not on the subject of arrays but RECTs.

    if a RECT is no initialised are the values 0 as default?

    is it possible to store ?
    array[32][3] = null;
    array[32][4] = null;
    array[32][5] = null;
    If array is defined as RECT array[something][something] it's an array of RECT not an array of pointers to rect. So the values in the array will be unitialised, but you can't assign 0 to them because they are not pointers.

    What you would need is something like this
    Code:
    array[x][y] = RECT(); // assuming rect has a default constructor
    Last edited by sigfriedmcwild; 05-15-2006 at 03:38 PM.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    903
    Uh?

    If the index variable (i) is lower than 5, i != 5 is equivalent to i < 5. If you use i != 4 it will stop one iteration too early.

    And by the way in c++ it's not null, or NULL it's 0. NULL is defined in some of the c headers but it is not (if I recall correctly) part of the c++ standard.
    I don't know why I said that O_o Anyway, NULL is defined in the windows.h header.

    Edit: It seems it is included in other headers as I did not include windows.h in my test file (I included iostream) and NULL was declared. I removed it (so it included no headers at all) and then it complained about NULL not being declared.

  13. #13
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    NULL is a preprocessor macro that expands to 0.
    It is quite possible that a header will declare it or include another header that declared it, but, if I recall correctly, it is not guaranteed by the standard.

    And no windows.h is not part of any standard.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    903
    I know windows.h is not standard. I never said that. I also know it is a preprocessor macro that is defined as 0.

  15. #15
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    I was just making sure it was clear to everyone, sorry if I offended you

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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