Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 10-10-2009
Member
 
Join Date: Apr 2009
Posts: 87
Function that returns a 2d array

A function can return a 2d array?Can one some give a explanation of this with example?

I tried the following code but does not work...
Code:
int function (array [] [120]) 
  ( 
  / / program 
  return array [] [120]; 
  )
Reply With Quote
  #2  
Old 10-10-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Function that returns a 2d array

Is it possible in C++ to have a function that returns a 2-D or indeed an X-D array from a function.Another alternative is to pass in the 2D array as a parameter into the function (by reference) and then filll it and then just leave the function which would maintain the array's content.
Reply With Quote
  #3  
Old 10-10-2009
Member
 
Join Date: Jan 2009
Posts: 199
Re: Function that returns a 2d array

Return a 2D array from a function in C :
Code:
#include 

struct array_def 
{ 
int **q; 
int row1; 
int col; 
}; 

int** func(); 

array_def* func_struc(); 

void main() 
{ 
int **b = func(); 
for(int j=0;j < 2; ++j) 
{ 
for(int k=0;k < 2; ++k) 
cout<cout<} 

array_def *pArr = func_struc(); 

for(j =0; j < pArr->row; ++j) 
{ 
for(int k =0; k< pArr->cols; ++k) 
cout<p[j][k]<cout<} 


} 

int** func() 
{ 
int **q; 
int cnt =7; 
q = new int*[2]; 
for(int j =0;j < 2; ++j) 
p[i] = new int[2]; 

for(j =0; j < 2; ++j) 
for(int k=0; k < 2; ++k) 
p[j][k] = cnt++; 

return q; 
} 

array_def* func_struc() 
{ 
array_def *pArray = new array_def; 
int cnt =1; 
pArray->q = new int*[2]; 
for(int j =0;j < 2; ++j) 
pArray->p[j] = new int[2]; 

for(j =0; j < 2; ++j) 
for(int k=0; k < 2; ++k) 
pArray->p[j][k] = cnt++; 

pArray->cols =2; 
pArray->row = 2; 

return pArray; 
}
Reply With Quote
  #4  
Old 10-10-2009
Member
 
Join Date: Dec 2008
Posts: 177
Re: Function that returns a 2d array

In your case you pass an array argument (but bad because you did not indicate the type), this table will be modified directly, so it is unnecessary to return.

A dynamically allocated 2D array can be returned by cons because it is a pointer to pointer.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Function that returns a 2d array"
Thread Thread Starter Forum Replies Last Post
Pass Array into a function Viensterrr Software Development 3 24-12-2009 12:55 PM
Two dimensional array to a function Chrisch Software Development 3 27-11-2009 12:47 PM
How to return an array from function Rilex Software Development 3 02-10-2009 09:18 AM
function that takes a 2D array as a parameter Umang Software Development 4 06-05-2009 11:59 PM
Function that returns an array ByteCoder Software Development 3 17-04-2009 07:02 PM


All times are GMT +5.5. The time now is 11:00 AM.