passing a 2dim array to a funct

This is a discussion on passing a 2dim array to a funct within the C++ Programming forums, part of the General Programming Boards category; Code: int funct(char *[][]) something like this won't work, and the books that i have don't really go into the ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    106

    passing a 2dim array to a funct

    Code:
    int funct(char *[][])
    something like this won't work, and the books that i have don't really go into the subject. any help would be appreciated. thanks in advance.

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    by doing that you are actually passing through a 3d array. If you want to pass in a 2d do this:
    Code:
    void foo( char p[][100] )
    { 
         //
    }
    where 100 is the size of the second dimension.

    edit: the 100, could be any number and SHOULD be the size of YOUR array's second dimension

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    great! i thought i had seen something like that somewhere, but i just couldnt recall it. thanks again.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    > could be any number and SHOULD be the size of YOUR array's second dimension
    All minor dimensions MUST match.

    There are 3 different ways of writing the same thing when it comes to passing multi-dimensional arrays to functions.
    Code:
    /* all these are the same */
    void foo( char p[50][100] );  /* size of 1st dim is entirely optional */
    void foo( char p[][100] );
    void foo( char (*p)[100] );   /* watch the (), char *p[100] is a different animal */
    This is handy, because the first one permits a rather simple and dead easy to remember rule called "copy and paste"

    If you had
    int arr[X][Y][Z];

    You can simply copy and paste that into a function parameter
    void foo ( int arr[X][Y][Z] );

    And call it with
    foo( arr );

    Just like you would do for any non-array type really

    If you want to go round chopping the X out of the function definition and declaration, then that is up to you. Though some lint tools will remark that the X size is redundant.
    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. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  2. passing a 2dim dynamically allocated array to a funct
    By agerealm in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2004, 05:55 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM

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