pass the pointer of a two dimension array to a function

This is a discussion on pass the pointer of a two dimension array to a function within the C Programming forums, part of the General Programming Boards category; Hi all, how to pass the pointer of a two dimension array to a function? Is it something like Code: ...

  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
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    I always thought 2D arrays and pointers to pointers were interchangable but I just tried it and it seems they're not.
    MSVC++ 6.0

  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
    712
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Quote Originally Posted by Micko
    Here is something worth to know
    I always pass 1D arrays as pointers though...
    MSVC++ 6.0

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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.
    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-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, 08:32 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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