Thread: Quick question about passing 2d arrays

  1. #1
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84

    Quick question about passing 2d arrays

    Suppose I have this code:

    Code:
    #include <iostream>
    
    void foo(int a[][3]);
    int main()
    {
        int num[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
       //size of array calculated in here
        foo(num);
        
        
        std::cin.get();
        
        
    }
    void foo(int a[][3])
    {
        std::cout<<a[2][1];
        return;
        
    }
    What if I don't know that the number of columns is going to be 3 for the two dimensional array? I just know in the function prototype that I need to receive a two dimensional array and I have another variable that will be used for the size of the array? So it would look more like this:

    Code:
    #include <iostream>
    
    void foo(int a[][], int size);
    int main()
    {
        int num[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
        //size calculated in here
        foo(num);
        
        
        std::cin.get();
        
        
    }
    void foo(int a[][], int size)
    {
        std::cout<<a[2][1];
        return;
        
    }
    I have a program where the size of the puzzle is determined by another file (crossword puzzle) so the program isn't very generic when everytime I want to do another puzzle I have to hard code what the size of the puzzle will be in the function prototype/function definition when it can be easily calculated from the files. Thanks.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    That is exactly one of the main advantages of STL containers over arrays.

    http://www.cprogramming.com/tutorial/stl/vector.html
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It would work better like this

    void foo(int **a, size_t cols, size_t rows);

    that way you don't have any guesswork to do about the size of the array and you're doing things right.

    [edit] double pwnt. Read up!

  5. #5
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Thanks guys.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void foo(int **a, size_t cols, size_t rows);
    NO NO NO!!!
    You can't pass a 2D array to a function expecting a pointer to a pointer. Your compiler should warn you, and if it doesn't you need a better one.
    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. Passing 2D dynamic arrays
    By s_siouris in forum C Programming
    Replies: 5
    Last Post: 11-12-2008, 08:08 AM
  2. passing dynamicaly allocated 2d arrays
    By s_siouris in forum C Programming
    Replies: 6
    Last Post: 05-25-2008, 04:19 PM
  3. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  4. Just a quick question about character arrays
    By Welshy in forum C Programming
    Replies: 3
    Last Post: 04-03-2006, 07:20 AM
  5. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 07:33 AM