Thread: passing arrays to functions

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    passing arrays to functions

    as we know to pass an array to a function the syntax is

    datatype function_name(type array_name[][size2],int size1,int size2).
    this is for a 2D single array.So what about if we want to pass multiple 2D array?whether it shall be like this

    datatype function_name(type array_name1[][size2],int size1,int size2,type array_name2[][size2],int size1,int size2)

    or shall it be some other type.Please help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Yep, just like that.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why the need for two size2's ?

  4. #4
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by Adak View Post
    Why the need for two size2's ?
    these two size2's are used in case the 2D arrays are of different sizes.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    If you're passing in a multidimensional array, you only need to pass in the size of the first dimension, as you're forced to specify the sizes of the remaining dimensions. That is:
    Code:
    void f(int a[][5][10], size_t n);
    You can only pass in arrays that look like:
    Code:
    int a[n][5][10];
    where n is whatever size you want. The second dimension will always be 5 and the third 10. You can pass those sizes in, but there's not any particular need to. You can always compute the sizes with sizeof if you don't want to use magic numbers, and this will guarantee the right values.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM
  4. passing arrays through functions
    By Ecko in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 08:21 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM