Passing a 2 dimensional array to a Function

Discussion in 'C++' started by poojithareddy@gmail.com, Nov 11, 2005.

  1. Guest

    Hai,
    I am trying to pass a 2 dimensional array to a function and I am having
    a problem with this.
    If the array was a Static 2D array, the code would be somethinlike this

    void displayArray(myArray[][10],int rowSize)

    If it is a Dynamic array, the number 10 for the second dimension cannot
    be mentioned and I dont know a workaround. Please let me know asap.

    Thank You,
    Poojitha.
    , Nov 11, 2005
    #1
    1. Advertising

  2. wrote:
    > Hai,
    > I am trying to pass a 2 dimensional array to a function and I am having
    > a problem with this.
    > If the array was a Static 2D array, the code would be somethinlike this
    >
    > void displayArray(myArray[][10],int rowSize)
    >
    > If it is a Dynamic array, the number 10 for the second dimension cannot
    > be mentioned and I dont know a workaround. Please let me know asap.
    >
    > Thank You,
    > Poojitha.
    >


    Normally this

    void displayArray(int **myArray, int rowSize, int colSize)

    but really it depends on how you have created your 2D dynamic array. Why
    not post the code you have written.

    john
    John Harrison, Nov 11, 2005
    #2
    1. Advertising

  3. Guest

    Your function would look something like this:

    void displayArray(int** array, int numCols, int numRows);

    you just tell it how many rows and columns are in the array.
    , Nov 11, 2005
    #3
  4. wrote:
    > Hai,
    > I am trying to pass a 2 dimensional array to a function and I am having
    > a problem with this.
    > If the array was a Static 2D array, the code would be somethinlike this
    >
    > void displayArray(myArray[][10],int rowSize)
    >
    > If it is a Dynamic array, the number 10 for the second dimension cannot
    > be mentioned and I dont know a workaround. Please let me know asap.


    Use 'vector<vector<whatever> >'

    V
    Victor Bazarov, Nov 11, 2005
    #4
  5. wrote:
    > ...
    > I am trying to pass a 2 dimensional array to a function and I am having
    > a problem with this.
    > If the array was a Static 2D array, the code would be somethinlike this
    >
    > void displayArray(myArray[][10],int rowSize)
    >
    > If it is a Dynamic array, the number 10 for the second dimension cannot
    > be mentioned and I dont know a workaround. Please let me know asap.
    > ...


    C++ does not have a core language data type, which would immediately represent a
    2D array with flexible second dimension. This means that in order to represent
    your 2D array you must be using a library- or user-defined data structure. The
    answer to your question heavily depends on what you are actually using. Post
    more details.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 11, 2005
    #5
  6. Victor Bazarov wrote:
    > wrote:
    > > Hai,
    > > I am trying to pass a 2 dimensional array to a function and I am having
    > > a problem with this.
    > > If the array was a Static 2D array, the code would be somethinlike this
    > >
    > > void displayArray(myArray[][10],int rowSize)
    > >
    > > If it is a Dynamic array, the number 10 for the second dimension cannot
    > > be mentioned and I dont know a workaround. Please let me know asap.

    >
    > Use 'vector<vector<whatever> >'
    >
    > V


    under NO circumstances use 'vector<vector<whatever> >'
    but rather 'vector<vector<whatever> >& '
    puzzlecracker, Nov 11, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Venkat
    Replies:
    4
    Views:
    969
    Venkat
    Dec 5, 2003
  2. seemanta dutta

    passing 2-Dimensional Array to a function

    seemanta dutta, Aug 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    7,864
    Eric Sosman
    Aug 8, 2003
  3. Sac
    Replies:
    15
    Views:
    8,255
    Barry Schwarz
    Jan 24, 2009
  4. Olumide
    Replies:
    6
    Views:
    740
    John Bode
    May 17, 2010
  5. Kai-Uwe Bux
    Replies:
    2
    Views:
    319
    Luc Danton
    Oct 3, 2010
Loading...

Share This Page