Passing arrays to functions?

This is a discussion on Passing arrays to functions? within the C++ Programming forums, part of the General Programming Boards category; I cant find out how it's the syntax. func(int [][] a) doesnt work... any hint? Thanks!...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    22

    Passing arrays to functions?

    I cant find out how it's the syntax.

    func(int [][] a) doesnt work... any hint?

    Thanks!

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    For multidimensional arrays I believe you have to specify the size in the function declaration and implementation (maybe not in the declaration?).

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    Thanks, checking now.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you know the size at compile time, you specify the size (although the first dimension isn't necessary). If you don't know the size, or the array is dynamically allocated, you should use a vector instead. Otherwise you'll have to pass the size as an extra function parameter and pass the array by pointer.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    func(int [][] a)
    In C/C++, you have to put array indicies ([]) after the variable name, unlike in Java where you can put them before or after. And of course, as mentioned, all but the leftmost index must have sizes in them.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > func(int [][] a) doesnt work... any hint?
    Copy / paste works

    If your array is
    int arr[X][Y][Z];

    Then the function can(*) be written as
    void func ( int arr[X][Y][Z] );

    And you would call it with
    func( arr );


    (*) Some compilers will warn you about the redundant major dimension (the left-most one), so you can easily change it to
    void func ( int arr[ ][Y][Z] );

    If you want to spend extra effort, you can write it like this if you want
    void func ( int (*arr)[Y][Z] );

    All 3 forms are equivalent.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 05: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, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21