Passing a 2 dimensional array to a Function

P

poojithareddy

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.
 
J

John Harrison

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
 
J

jperalta17

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.
 
V

Victor Bazarov

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
 
A

Andrey Tarasevich

...
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.
 
P

puzzlecracker

Victor said:
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> >& '
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top