Dynamically allocated arrays

This is a discussion on Dynamically allocated arrays within the C Programming forums, part of the General Programming Boards category; i wanted to know how would you create dynamically allocated 2D arrays. i understand how to create 1D arrays but ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    Question Dynamically allocated arrays

    i wanted to know how would you create dynamically allocated 2D arrays. i understand how to create 1D arrays but don't know how to do that.

    Thanks

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    well do it for one, but twice
    Code:
         int *ptr,x;
         ptr=malloc(10*sizeof int);
         if(!ptr){
              perror("Something went wrong\n");
              return 1;
         }
         for(x=0;x<10;x++){
              ptr[x]=malloc(10*sizeof int);
                   if(!ptr[x]){
                       perror("Something went wrong\n");
                       return 1;
                   }
        }

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    linuxdude it's int **ptr;
    byt anyway, it's quite limiting, because you are forced to use indexing. I wold probably go for
    Code:
    int *tbl = malloc(sizeof(*tbl)*TBL_N*TBL_M);
    tbl[n*TBL_M+m] = a;
    free(tbl);

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by axe786
    i wanted to know how would you create dynamically allocated 2D arrays. i understand how to create 1D arrays but don't know how to do that.
    http://www.eskimo.com/~scs/C-faq/q6.16.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    whoops sorry about that didn't really pay attention.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    Thanks for the reply guys, i'm just a little confused on this line:
    Code:
    tbl[n*TBL_M+m] = a;
    wats (n),(m) and (a) and also what excatly does this statement do???

    Thanks

  7. #7
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by linuxdude
    well do it for one, but twice
    Code:
    ...
    In Java or C# they call that thing a jagged array because your first array stores pointers to other arrays, so your second dimension could be different for each element of the first one. If you look at the memory layout, it's not anything that could be interpreted as "2D" either, but maybe it's still called a 2d-array. Is it?

    Quote Originally Posted by axe786
    Thanks for the reply guys, i'm just a little confused on this line:
    Code:
    tbl[n*TBL_M+m] = a;
    wats (n),(m) and (a) and also what excatly does this statement do???

    Thanks
    You're basically allocating a one dimensional array that is big enough to hold enough elements for the 2 dimensions you wanted. TBL_M is a constant for the size of the first dimension m. So for an array of the size [2][3] your flat array would hold its elements in this order

    a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2]
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. swap dynamically allocated arrays
    By myle in forum C Programming
    Replies: 9
    Last Post: 05-18-2008, 09:04 AM
  2. 2D Dynamically allocated pointer arrays
    By Lionmane in forum C Programming
    Replies: 37
    Last Post: 06-11-2005, 10:39 PM
  3. scope of dynamically allocated variables
    By lanzyzhang in forum C Programming
    Replies: 4
    Last Post: 07-20-2004, 10:59 AM
  4. Destructors in dynamically allocated arrays
    By frenchfry164 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 10:26 PM

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