three dimenssional arrays

This is a discussion on three dimenssional arrays within the C Programming forums, part of the General Programming Boards category; Can somebody please tell me how do I use three dimenssional arrays. eg char ***array...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    1

    three dimenssional arrays

    Can somebody please tell me how do I use three dimenssional arrays. eg char ***array

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    A simple example using a dynamic 3D array:
    Code:
    #include <stdio.h>
    
    const int a = 10;
    const int b = 4;
    const int c = 100;
    
    int main(void)
    {
       int i, j;
       char ***array;
    
       /* allocate memory */
       array = (char ***) malloc(a * sizeof(*array));
    
       for(i = 0; i < a; i++)
       {
          array[i] = (char **)malloc(b * sizeof(**array));
          for(j = 0; j < b; j++)
             array[i][j] = (char *)malloc(c * sizeof(***array));
       }
    
       /* fill array */
       for(i = 0; i < a; i++)
          for(j = 0; j < b; j++)
             sprintf(array[i][j], "%d - %d", i, j);
    
       /* print array */
       for(i = 0; i < a; i++)
          for(j = 0; j < b; j++)
             printf("%s\n", array[i][j]);
    
       /* free memory */
       for(i = 0; i < a; i++)
       {
          for(j = 0; j < b; j++)
             free((void *)array[i][j]);
          free((void *)array[i]);
       }
    
       free((void *)array);
    
       return 0;
    }

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If you are asking because you say code that has a char ***array in it then I should point out that this is probably not a three dimensional array. You'll usually see this when one needs a pointer to a 2d array or a pointer to a pointer to an array. Using a char *** as a parameter in a function which allocates memory for a char ** is not uncommon.

    Code:
    void ArrayAlloc(char ***array, int width, int height) {
        int w;
        *array = (char **array)malloc(height*sizeof(char *));
        for(;--height;)
            (*array)[height] = (char *)malloc(width);
    }
    
    int main(void) {
        char **array;
        ArrayAlloc(&array, 20, 20);
        return 0;
    }

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