sending arrays into functions

This is a discussion on sending arrays into functions within the C++ Programming forums, part of the General Programming Boards category; hi, this is probably a simple question but how would i send a multidimensional array called myArray[][] into a function...i ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    sending arrays into functions

    hi, this is probably a simple question but how would i send a multidimensional array called myArray[][] into a function...i understand how to do it if the array is like myOtherArray[]

    i'd rather not have to change myArray[][] into myOtherArray[] since the double subscripts [][] are easy for me to work with than the single one []

    this is basically my attempt at doing this:

    Code:
    void Tools(int myArray[][]);
    
    int main()
    {
    int theArray[12][2];
    
    for(k=0; k<12; k++)
    {
    	for(i=0; i<2; i++)
    	{
    		theArray[k][i] = i+k;
    	}
    }
    
        Tools(theArray);
    
        return 0;
    }
    
    void Tools(int myArray[][])
    {
    
    ...
    //stuff happens
    ...
    
    }

    thanks for all your help

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You cannot pass a two dimensional array into a function like that. Specify the size of the second dimension (or use a 2-d vector).

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Copy/paste works.

    If you have this
    int theArray[12][2];

    The function prototype/definition can be written as
    void Tools(int theArray[12][2])


    > void Tools(int **Array);
    This is VERY wrong - they're arrays, not pointers - don't let the fact you can use [][] on both of them indicate any sort of equivalence.
    If you wanted pointer notation, it would be
    void Tools(int (*Array)[2] );
    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.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    hey thanks for the help guys i think i got it now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  2. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  3. Arrays and Functions
    By w274v in forum C Programming
    Replies: 19
    Last Post: 11-03-2005, 01:47 PM
  4. functions and arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 09:57 AM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 02:48 AM

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