1. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Rep Power

    passing multidimensional arrays to functions

    i have always passed multidimensional arrays to function as pointers, b/c imho that's what they REALLY are. this code for example:
    void foo(char ***x)
            //x holds address of y
            for(x; *x != NULL; x = &(*x++)){
                    printf(" %s ", *x);
    void bar(int **y)
            for(y; *y != NULL; y = &(*y++)){
                    printf("  %d  ", *y);
    int main(void)
            //y is typed as a pointer to a character * array
            char    *y[] = { "hi", "there", "sean", "!\n", NULL };
            foo((char ***)&y);
            int     x[2][2] = { 5, 4, 3, 0 };
            bar( (int **)x);
    then i just read this:
    Finally, we might explicitly note that if we pass a multidimensional array to a function:

    int a2[5][7];

    we can not declare that function as accepting a pointer-to-pointer:

    func(int **a) /* WRONG */

    As we said above, the function ends up receiving a pointer to an array, not a pointer to a pointer.
    so what's that all about? i understand the typage issues, but that's why it gets casted. and that last line, "ends up recieving a pionter to an array"; well an array IS a pointer to a block of some type, yes? so we are passing a POINTER to a POINTER...makes sense to me
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Rep Power
    I think it is C/C++ compiler hoo haa. I don't have any problems doing the *** thing in C, but sometimes in C++, particularly if I hard-code the array, I get lots of heartburn and it insists I use [][][]. I believe if you define the pointer as ***, then malloc memory for it, you can pass it as ***. Dunno why the damn thing is so pissed about it though. I believe in the end it is all semantics.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Rep Power
    When you declare an array of integers you have ... an array of ints.

    When you pass an array of ints to a function there is a conversion to a pointer to the first element, i.e. a pointer to int.

    When you declare a multidimensional array (e.g. of ints) you have an array of arrays (of ints), not an array of ints.

    When you pass an array of arrays to a function there is a conversion to a pointer to the first element of the array, and the first element is an array (of ints), not an int, so the conversion is to a pointer to an array (of ints).

    I believe that's what the author is referring to.

    Footnote:- conceptually it's often useful to think of an array in the same way as we think of a pointer because the two behave the same under some circumstances. Unfortunately it's all too easy to forget that an array is *not* actually a pointer.
    Last edited by BigBadBob; October 10th, 2003 at 02:36 PM.

IMN logo majestic logo threadwatch logo seochat tools logo