Thread: Not understanding the concept of multidimensional arrays and functions

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    Not understanding the concept of multidimensional arrays and functions

    Hi,
    I am having alot of trouble with the concept of multidimensional arrays and functions.

    for one dimensional arrays, when calling the function, we use the function name and number of elements. But this does not seem to work with multidimensional array.

    note: I have not used pointers. I have seen some sample programs with pointers but do not understand them yet

    example program
    Code:
    #include <stdio.h>
    
    #define LEN 10
    
    int sum(int a[][LEN], int n)
    {
    	//do something
    }
    
    int main()
    {
    	int b[10][LEN], total;
    
    	total = sum(b[][LEN], 10); //how do I call sum function here?
    
    	getchar();
    	return 0;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Just pass the name of the array.
    Code:
    #include <stdio.h>
     
    #define ROWS 5
    #define COLS 10
    
    int sum(int a[][COLS], int rows)
    {
        // ...
    }
     
    int main()
    {
        int b[ROWS][COLS], total;
     
        total = sum(b, ROWS);
     
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    noted.
    I see that if I declare the rows and cols in the function definition for array a (i.e. have only 1 argument), then I only need to pass the name in the function main..

    for example.

    Quote Originally Posted by oogabooga View Post
    Just pass the name of the array.
    Code:
    #include <stdio.h>
     
    #define ROWS 5
    #define COLS 10
    
    int sum(int a[ROWS][COLS])
    {
        // ...
    }
     
    int main()
    {
        int b[ROWS][COLS], total;
     
        total = sum(b);
     
        return 0;
    }
    Why this this the case? Perhaps I may need to learn pointers to understand this?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Actually, ROWS in the function definition does nothing at all. It's the same with or without it. The only reason you don't have to pass the number of rows as a parameter is that the constant ROWS is visible to the function.

    I passed ROWS to the function simply because the function will work with a 2D array with any number of rows, but the array must have COLS columns.

    EDIT: So the two ways you could code it are
    Code:
    int sum(int a[][COLS], int rows)
    {
        int r, c;
        for (r = 0; r < rows; r++)
            for (c = 0; c < COLS; c++)
                // ...
    }
    or
    Code:
    int sum(int a[][COLS])
    {
        int r, c;
        for (r = 0; r < ROWS; r++)
            for (c = 0; c < COLS; c++)
                // ...
    }
    But it's never necessary (or even useful) to put a number in the first array dimension in the function header.
    Last edited by oogabooga; 09-24-2013 at 06:56 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional arrays and functions.
    By miniwhip in forum C Programming
    Replies: 6
    Last Post: 09-12-2007, 01:27 PM
  2. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM
  3. Trivial Trigonometry Question, Need Help Understanding Concept
    By SourceCode in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 12-14-2003, 05:50 PM
  4. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM
  5. passing multidimensional arrays to functions
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 03:27 AM

Tags for this Thread