Thread: Arbitrary length multi-dimensional arrays

  1. #1
    Registered User
    Join Date
    Jun 2006

    Arbitrary length multi-dimensional arrays

    Newbie question here. It's been a while since I've done C programming,
    and I hit a wall last night.

    Let's say I have a three dimensional array, like so:
    int p[2][3][3] =
    I also have a number of other three dimensional arrays, generated with
    a code generating script. The size of each dimension varies with each
    one. Some are [5][3][3], some are [2][5][5], etc...

    How would I declare a variable that could hold any of these
    3-dimensional arrays? I'm tripping over the pointer syntax.

    I'd like to be able to do something like:
    int ***val = p; //The variable p from the last example.

    I'm pretty sure that ***val is the wrong way to go about it.

    Thanks for your help!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    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.*

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    One way would be dynamic memory allocation. Let's say your program knows how big each dimension is and they're stored in the variables s_dim1, s_dim2, s_dim3. You could do something like this:
      int ***p;
      int s_dim1 = 4, s_dim2 = 5, s_dim3 = 2;
      int i, j;
      p = malloc(sizeof(*p) * s_dim1);
      for(i = 0;i < s_dim1;++i)
        p[i] = malloc(sizeof(**p) * s_dim2);
        for(j = 0;j < s_dim2;++j)
          p[i][j] = malloc(sizeof(***p) * s_dim3);
    There isn't any way to just make an array in C without giving it a size, so the dynamic memory allocation is probably the best way to go.

    Disclaimer: I didn't test the above code, but it should work.
    Last edited by itsme86; 06-26-2006 at 11:26 AM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    May 2006
    Here is code that's tested for two dimensional array

    int **array1 = (int **)malloc(nrows * sizeof(int *));
    for(i = 0; i < nrows; i++)
    array1[i] = (int *)malloc(ncolumns * sizeof(int));

    access allocated 2D array like: array1[1][2] = 5;

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    itsme86's answer is much better.
    It avoids casting the result of malloc, and avoids explicit reference to sizeof(type).

    Though it is possible to allocate all 3 dimensions with just 3 malloc's.
    Here is a recent example for 2 dimensions, just to give you the idea.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  2. multi dimensional vectors
    By r0bbb in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2005, 08:14 AM
  3. Passing 2 dimensional arrays to functions
    By homeyg in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2005, 03:16 PM
  4. How to declare global arrays of unknown length?
    By ajm218 in forum C Programming
    Replies: 3
    Last Post: 08-07-2003, 09:13 AM
  5. turning strings of unknown length into arrays
    By hankspears in forum C Programming
    Replies: 3
    Last Post: 04-11-2002, 12:16 PM