By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

how to pass a 2D array to a function C++

P: 11
hiii
i want to pass a 2D array to a function .. the size of the array is not fixed .. i have tried using pointer to the array ..but it is giving error ..
what i wrote was :
Expand|Select|Wrap|Line Numbers
  1.        void fun(int *,int,int);
  2. int main()
  3. {
  4.  
  5. int ht,wd;
  6. cin>>ht>>wd;
  7. int arr[ht][wd];
  8. int fun(&arr,ht,wd);
  9. for(int k=0;k<ht;k++)
  10. {
  11.  
  12.     for(int l=0;l<wd;l++)
  13.     {
  14.     cout<<arr[k][l];
  15.     }
  16. }
  17.  
  18. }
  19.  
  20. void fun(int*a,int h,int w)
  21. {
  22. for(int i=0;i<h;i++)
  23. {
  24.     for(int j=0;j<w;j++)
  25.     {
  26.         cout<<"roe"<<i<<"  ";
  27.         cin>>*(a+i*w+j);
  28.     }
  29. }
  30. }
  31.  
can some1 help me wid dis .. i m fairly new to c++
Jun 13 '07 #1
Share this Question
Share on Google+
5 Replies


P: 94
Please remember CODE tags

I'm not really seeing what you are asking, or what your code is doing
I would suggest that you get to know pointers first
You are asking how to pass a 2D array to a function C++
Wel, here is some code for that

First declare a 2D array
Expand|Select|Wrap|Line Numbers
  1. int ht=0, wd=0;
  2.     cout << "Please enter the size of the array:  " << endl;
  3.     cin >> ht;
  4.     cin >> wd;
  5.  
  6.     int **array;               // Declaration, however, your declaration is also correct
  7.  
  8.     *array = new int[ht]; // Allocate some memory
  9.     for(int i = 0; i < ht; i ++)
  10.     {
  11.         array[i] = new int[wd];
  12.         for(int j = 0; j < wd; j ++)
  13.         {
  14.             array[i][j] = 0;
  15.         }
  16.  
  17.     }
  18.  
Passing that as an argument to a function would be :
Expand|Select|Wrap|Line Numbers
  1. void fun(int ** array, int ht, int wd)
  2. {
  3.     for(int i = 0; i < ht; i ++)
  4.     {
  5.         for(int j = 0; j < wd; j ++)
  6.         {
  7.             cout << array[i][j] << "\t";
  8.         }
  9.         cout << endl;
  10.     }
  11. }
calling that in the main function :
Expand|Select|Wrap|Line Numbers
  1.   fun(array, ht, wd);
Jun 13 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This is not correct:

I'm not really seeing what you are asking, or what your code is doing
I would suggest that you get to know pointers first
You are asking how to pass a 2D array to a function C++
Wel, here is some code for that

First declare a 2D array

Code: ( text )
int ht=0, wd=0;
cout << "Please enter the size of the array: " << endl;
cin >> ht;
cin >> wd;

int **array; // Declaration, however, your declaration is also correct

*array = new int[ht]; // Allocate some memory
for(int i = 0; i < ht; i ++)
{
array[i] = new int[wd];
for(int j = 0; j < wd; j ++)
{
array[i][j] = 0;
}

}

Passing that as an argument to a function would be :

Code: ( text )
void fun(int ** array, int ht, int wd)
{
for(int i = 0; i < ht; i ++)
{
for(int j = 0; j < wd; j ++)
{
cout << array[i][j] << "\t";
}
cout << endl;
}
}

calling that in the main function :

Code: ( text )
fun(array, ht, wd);
An int** is a pointer-to-a-pointer-to-a-single-int. It is not a pointer to a 2D array.

For example, this array:
Expand|Select|Wrap|Line Numbers
  1. int arr[3][4];
  2.  
has three elements. Each element is an array of 4 int. Now, the name of the array (arr) is the address of element 0. Therefore,the name of the array is trhe address of an array of 4 int. The address of an array of 4 int is declared as:

Expand|Select|Wrap|Line Numbers
  1. int (*ptr)[4];
  2.  
That is what you use in your function calls. You will need a second argument for the number of these arrays in the array used on the call:
Expand|Select|Wrap|Line Numbers
  1. void MyFunction(int (*ptr)[4], int num)
  2. {
  3.  
  4. }
  5.  
  6. int main()
  7. {
  8.     {
  9.         int arr[3][4];
  10.         MyFunction(arr, 3);
  11.  
  12.     }
  13. }
  14.  
If your array is allocated on the heap using the new operator, the address returned will be the address of an array of 4 int:

Expand|Select|Wrap|Line Numbers
  1. int (*arr)[4]   = new int[3][4];
  2.  
Jun 13 '07 #3

emaghero
P: 85
To pass a 2D array to a function proceed as follows

1. Declare memory for the array

Expand|Select|Wrap|Line Numbers
  1. int row,column;//Declare variables to hold the size of your array
  2.  
  3. cout<<"Input the size of your array\n";
  4. cout<<"\n";
  5. cout<<"Input the number of rows\n";
  6. cin>>row;
  7. cout<<"Input the number of columns\n";
  8. cin>>column;
  9.  
  10. double *arr=new(double [row*column]);
  11. //This declares memory for a dynamically allocated array of doubles
  12.  
  13. //It is always a good idea to initialise the array to zero before you use it
  14. for(int i=0;i<row;i++){
  15.     for(int j=0;j<column;j++){
  16.          *(arr+i*column+j)=0.0;
  17.     }
  18. }
  19.  
  20.  
2. Define the function to which the array is to be passed

Expand|Select|Wrap|Line Numbers
  1. void function(double *array,int number_of_rows, int number_of_columns)
  2. {
  3.          //This function will print the array to the screen
  4.           for(int i=0;i<number_of_rows;i++){
  5.               for(int j=0;j<number_of_columns;j++)
  6.                    cout<<*(array+i*number_of_columns+j)<<" ";
  7.                    cout<<"\n";
  8.           }
  9. }
  10.  
3. Pass the array to the function by calling it

Expand|Select|Wrap|Line Numbers
  1. //Perform some operation on the array here
  2.  
  3. //Call the function which passes the array
  4. function(arr,row,column);
  5.  
  6.  
Jun 14 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
To pass a 2D array to a function proceed as follows

1. Declare memory for the array


Code: ( text )
int row,column;//Declare variables to hold the size of your array

cout<<"Input the size of your array\n";
cout<<"\n";
cout<<"Input the number of rows\n";
cin>>row;
cout<<"Input the number of columns\n";
cin>>column;

double *arr=new(double [row*column]); //This declares memory for a dynamically allocated array of doubles

//It is always a good idea to initialise the array to zero before you use it
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
*(arr+i*column+j)=0.0;
}
}
This is not allocating a 2D array. It is allocating a one-dimensional array.

Read my previous post.
Jun 14 '07 #5

P: 11
To pass a 2D array to a function proceed as follows

1. Declare memory for the array

Expand|Select|Wrap|Line Numbers
  1. int row,column;//Declare variables to hold the size of your array
  2.  
  3. cout<<"Input the size of your array\n";
  4. cout<<"\n";
  5. cout<<"Input the number of rows\n";
  6. cin>>row;
  7. cout<<"Input the number of columns\n";
  8. cin>>column;
  9.  
  10. double *arr=new(double [row*column]);
  11. //This declares memory for a dynamically allocated array of doubles
  12.  
  13. //It is always a good idea to initialise the array to zero before you use it
  14. for(int i=0;i<row;i++){
  15.     for(int j=0;j<column;j++){
  16.          *(arr+i*column+j)=0.0;
  17.     }
  18. }
  19.  
  20.  
2. Define the function to which the array is to be passed

Expand|Select|Wrap|Line Numbers
  1. void function(double *array,int number_of_rows, int number_of_columns)
  2. {
  3.          //This function will print the array to the screen
  4.           for(int i=0;i<number_of_rows;i++){
  5.               for(int j=0;j<number_of_columns;j++)
  6.                    cout<<*(array+i*number_of_columns+j)<<" ";
  7.                    cout<<"\n";
  8.           }
  9. }
  10.  
3. Pass the array to the function by calling it

Expand|Select|Wrap|Line Numbers
  1. //Perform some operation on the array here
  2.  
  3. //Call the function which passes the array
  4. function(arr,row,column);
  5.  
  6.  
thankyou .. i can do it nw
Jun 15 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.