Thread: pass the pointer of a two dimension array to a function

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    30

    pass the pointer of a two dimension array to a function

    Hi all,

    how to pass the pointer of a two dimension array to a function?

    Is it something like
    Code:
    int array[10][5];
    func( &array[0][0] );
    ...
    ...
    void func( int[][] *array ) {
        ...
    }
    Is the code void func( int[][] *array ) correct?
    Last edited by SoFarAway; 04-12-2005 at 06:56 AM.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    9
    i think its right.....

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    You need to pass a pointer that contains the start address of the multidimensional array to the function, not the array itself.
    When no one helps you out. Call google();

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    I always thought 2D arrays and pointers to pointers were interchangable but I just tried it and it seems they're not.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    8

    Thumbs up

    you could pass that kind of pointer with a struct, also.

    Code:
    struct array {
    char *rows;  //char stands for *type* 
    char *columns;
    } Array
    i.e., Array *arrayPtr...

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by samGwilliam
    I always thought 2D arrays and pointers to pointers were interchangable but I just tried it and it seems they're not.
    Here is something worth to know
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int array[10][5];
    > func( &array[0][0] );
    > ...
    > ...
    > void func( int[][] *array )

    Easy, just copy/paste the declarations

    int array[10][5]; // this is your array

    void func( array[10][5] ); // this is the prototype
    The array definition is exactly the same.

    func( array ); // this is the call, passing the array to the function

    You use the same [x][y] indexing inside the function as you would outside the function.
    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.

  8. #8
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by Micko
    Here is something worth to know
    I always pass 1D arrays as pointers though...
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Arrays are always passed as a pointer to the first element of the array, whether you say "int *" or "int []" in the prototype of the function.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM