Thread: Multi-dimensional Arrays

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    64

    Multi-dimensional Arrays

    I don't really understand the way multi-dimensional arrays works with function call.

    1) How to declare a function that would take a multi-dimensional array as parameter.

    Code:
    void fun(THIS PART);
    2) How to call the function.

    Code:
        main() {
            fun(THIS PART);
        }

    any help would be appreciated.

    --------------------------------------------


    Code:
    #include <stdio.h>
    
    void fun(int (*n)[][]);
    
    main() {
            int n[2][3] = {
                    { 11, 12, 13 },
                    { 21, 22, 23 }
            };
    
            fun(&n);
    }
    
    void fun(int (*n)[][]) {
            printf("%d\n", (*n)[1][1]);
    }
    Last edited by thinhare; 12-20-2004 at 09:23 PM.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    OK. I found an example to make it clear, the key of the solution is the size of columns should be defined when declaring the function:


    #include <stdio.h>

    void printArray(int array[][4]); /* declare function */

    int main() {
    int array[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
    printArray(array);
    return 0;
    }

    void printArray(int array[][4]) { /* define function */
    int i, j;

    for(i=0 ; i<3 ; i++) {
    for(j=0 ; j<4 ; j++) {
    printf("%2d ", array[i][j]);
    }
    printf("\n");
    }
    }
    Last edited by thinhare; 12-20-2004 at 09:24 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    salem, I looked into the link but didn't get it. have I done anything crappy?

    I've kept trying for hours before asking people here.

    And fortunately, I found the answer elsewhere, then posted here to share with people who may concern.

    Am I wrong?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    From the link:
    USE CODE TAGS!!!
    If you understand what you're doing, you're not learning anything.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    Looks like you answered your own question anyway.

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    This is your original code (as a reference):
    Code:
    #include <stdio.h>
    
    void fun(int (*n)[][]);
    
    main() {
    	int n[2][3] = {{ 11, 12, 13 },{ 21, 22, 23 }};
    	fun(&n);
    }
    
    void fun(int (*n)[][]) {
    	printf("%d\n", (*n)[1][1]);
    }
    Code:
    int n[2][3] = {{ 11, 12, 13 },{ 21, 22, 23 }};
    I really wished this worked, but I'm afraid we have to stream it like this:
    Code:
    int n[2][3] = {11, 12, 13, 21, 22, 23 };
    You had the function like this:
    Code:
    void fun(int (*n)[][])
    But that would be a uuhh..triarray multitwilight zone something... remember we're making this a grid type array (2 dimensional), not 3 dimensional. So it would come down to something like this:
    Code:
    void fun(int[][3]);
    And we're also going to have to remember the 3 as a column.

    And then you had:
    Code:
    printf("%d\n", (*n)[1][1]);
    Remember that's the same thing as saying:
    Code:
    printf("%d\n", n[0][1][1]);
    And that's wrong, because we aren't dealing with an array that's 3 dimensional, we're dealing with a two dimensional array, so the correct approach would be like this:
    Code:
    printf("%d\n", n[1][1]);
    And that would give us 22.

    Here's the final code:
    Code:
    #include <stdio.h>
    
    void fun(int[][3]);
    
    int main() {
    	int n[2][3] = { 11, 12, 13, 21, 22, 23 };
    	fun(n);
    }
    
    void fun(int n[][3]) {
    	printf("%d\n", n[1][1]);
    }

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    Oops! I got it. Let's test it:

    #include <stdio.h>

    void fun(int (*n)[][]);

    main() {
    int n[2][3] = {{ 11, 12, 13 },{ 21, 22, 23 }};
    fun(&n);
    }

    void fun(int (*n)[][]) {
    printf("%d\n", (*n)[1][1]);
    }

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    Code:
    Again

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    Uh, now I know how to work it out.

    Thanks everybody..

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. fscanf for multi dimensional arrays
    By rambos in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 03:26 AM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. Pointers to multi dimensional arrays.
    By bartybasher in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2003, 02:41 PM