Thread: Matrix on stack to int**... How?

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Dave Evans View Post
    TO make the parameter a pointer to an array[10][20] of ints, it should be:
    Code:
    void func(int (*arr2d)[10][20]);
    Of course, functions defined like this will only work for that particular size array (which, I think, will not help whatever it is that the Original Poster is trying to accomplish).

    D.
    That the same (essentially) as this:
    Code:
    void func(int arr2d[][10][20]);
    Passing an 2d array by reference like that makes sense, because if you have a required 2nd detention, chances are you have a required first detention. That syntax provides a way to force that requirement.

    Also, you can alternatively make it a template like this. Beware that this will not allow heap arrays to be passed as arguments:
    Code:
    template <typename T, int I> 
    void func(T (&array)[I]);
    Note: that I in brackets should be capital. For some reason the forum prevents that.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #17
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by laserlight View Post
    I think that your example offers no advantage over King Mir's example
    It isn't my example; I was just correcting what I thought was a (typographical) syntax error from the previous post.

    [edit]I was mistaken about the intent of the previous post. I regret my error.[/edit]

    In fact, in my experience, functions that deal with 2-D matrices almost always use pointers or pointers-to-pointers except for very specialized applications that have fixed-size matrices.

    Even fixed-size matrix functions (3x3 graphics manipulations, for example) might be more efficient with pointers, since element access is just a couple of indirect address operations instead of using the 2-D matrix calculations involving an addition and a multiplication.

    D.
    Last edited by Dave Evans; 10-11-2007 at 04:20 PM.

  3. #18
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Dave Evans View Post
    It isn't my example; I was just correcting the (typographical) syntax error from the previous post.

    In fact, in my experience, functions that deal with 2-D matrices almost always use pointers or pointers-to-pointers except for very specialized applications that have fixed-size matrices.

    Even fixed-size matrix functions (3x3 graphics manipulations, for example) might be more efficient with pointers, since element access is just a couple of indirect address operations instead of using the 2-D matrix calculations involving an addition and a multiplication.

    D.
    It may be worthwhile to pass a 2d stack by creating pointers to each row, and passing that as an array. that would mean doing something like this:
    Code:
    void func(int **array); //same as int *array[] 
    ...
    int main(){
        int array2d[3][10];
        int array2dasPtr={array2d[0],array2d[1],array2d[2]}
        func(array2dasPtr);
    }
    But that is not either of our codes were doing.
    To use your prototype you must do this:
    Code:
    void func(int (*arr2d)[3][10]);
    ...
    int main(){
        int array2d[3][10];
        int array3d[5][3][10]
        func(&array2d);
        func(array3d);
    }
    To use my prototype you must do this:
    Code:
    void func(int (&arr2d)[3][10]);
    ...
    int main(){
        int array2d[3][10];
        int array3d[5][3][10]
        func(array2d);
        func(array3d[0]);
    }
    Last edited by King Mir; 10-11-2007 at 04:16 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #19
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by King Mir View Post
    That the same (essentially) as this:
    Of course: you are correct; I misunderstood.

    D.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM