Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

c++ passing array from function to main function wit pointer

Status
Not open for further replies.

mengghee

Full Member level 3
Joined
Nov 29, 2005
Messages
163
Helped
8
Reputation
16
Reaction score
5
Trophy points
1,298
Location
United Kingdom
Activity points
3,105
Hi,

can anyone give me an example on how to pass array values out of a function to main function using pointers. I have been trying and trying ... sigh* cant get the right command. Thank you.

regards,
mengghee
 

Re: c++ passing array from function to main function wit poi

hmnn
couldn't find my answer there.

regards,
mengghee
 

Re: c++ passing array from function to main function wit poi

Can you be more specific on what your are trying to do
Are you trying to return a pointer from a function or are you trying to pass a pointer to a function as its argument.

Case 1: Function returns a pointer
double * AllocMem(int n); //declaration
//definition
double *AllocMem(int n)
{
double *var;
var = new double[n];
return var;
}

Case 2: Pointer as parameter to function
void ClearMem(double *var); //declaration
//definition
void ClearMem(double *var)
{
delete [] var;
return;
}
 

Refer any C eBook for further study ....
 

Re: c++ passing array from function to main function wit poi

go through yashwant kanatekar's "Let us C" book
 

Re: c++ passing array from function to main function wit poi

Thinking in c++ book has several exemples where you can find what you are looking for ,espesially in pointer`s area
 

jjohn, sanjiv - in C++ its different, not as in C.

I guess, it will make everything simple if you create a matrix template, as in the case of vector templates (which is inbuilt).

refer numerical recipes in C++ source code i have uploaded in the eda upload/download section for the template or skim thro the last few pages for the header file nrutil.h and nrtypes.h in NR in C++ book and construct the template... if you dint know how to construct yourself..

once this is done, you can pass the matrix to the function from the main as in say,

sample_function(mat_a, mat_b, k)

where mat_a and mat_b are matrices and k is some datatype.

and the function declaration will be,

sample_function(mat_IO_DP &mat_a, mat_IO_DP &matb, int k)

here, mat_a and matb can be of any names... and so is k.

and mat_IO_DP is the typecast for the syntax... (as followed in numerical recipes C++)

matrix<double> mat_a;

Its little tedious to create the h file with the template.. but once done, it will be handy in tons of situations trust me... you can also include many other matrix operations in it so that your programming becomes more efficient and economic (saves many lines)!!

good luck,
cedance.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top