Thread: how can i pass 2d arrays

  1. #1
    Unregistered
    Guest

    how can i pass 2d arrays

    i am trying to define function to accept 2d arrays like this

    bool abc(int def[][]);

    but vc is not letting me, with this error

    error C2087: '<Unknown>' : missing subscript

    does this means we can't pass 2d arrays?

  2. #2
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99
    try passig it as def[subscript1][subscript2]
    if you know them in advance
    otherwise use
    int** def
    jv

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You have to specify the first index:

    bool abc(int def[10][]);
    Last edited by Troll_King; 11-01-2001 at 08:30 PM.

  4. #4
    Unregistered
    Guest
    jasrajva(anybody), can you please given me an example of

    int** def

    specifying the first index would really limit the functionality.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Marvellous - 3 wrong answers in a row - at least Troll_King is on the right track.

    If you have
    int array[ROW][COL];

    Then the valid function prototypes are
    void foo ( int a[ROW][COL] );
    void foo ( int a[][COL] );
    void foo ( int (*a)[COL] );

    And you call this function like so
    foo ( array );

    You must specify all the substripts - EXCEPT the first one, which is entirely optional.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    Try this m8, this is a small example of how it is done

    #include <iostream.h>


    void read(int a[][5]);
    void print(const int a[][5]);


    int main()
    {
    int a[3][5];
    read (a);
    print(a);



    return 0;
    }

    void read(int a[][5])
    {

    cout << "Enter 15 integres, 5 per row:\n";
    for(int i = 0; i < 3; i++)
    {
    cout << "Row " << i << ": ";
    for(int j = 0; j < 5; j++)
    cin >> a[i][j];
    }
    }

    void print(const int a[][5])
    {

    for(int i = 0; i < 3; i++)
    {
    for(int j = 0; j < 5; j++)
    cout << " " << a[i][j];
    cout << endl;

    }

    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2009, 09:29 PM
  2. Having problems with 2d arrays
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2004, 11:46 AM
  3. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  4. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  5. Getting data from file using 2D arrays
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 09-07-2001, 03:12 PM