473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

function return dynamic array data

5
Dear All,

I would like to write a small function which will return a dynamic array.
The function is a simple function like this:
Expand|Select|Wrap|Line Numbers
  1. function_a(int a, int b)
  2. {
  3. }
but, it will return a dynamic array values like this:
peter 5 60.00
mary 6 70.20
...

How I do this in c++!? use function pointer ?? or other method !?
Moreover, how can I get the return values from the main() function!?
Since I am a newbie in C++, please provide some sample code. thanks a lot.
Jul 22 '07 #1
2 6044
sicarie
4,677 Expert Mod 4TB
I believe we need more information to help you. How were the values of the array created? How will the function have access to them? Is it supposed to create them? Are there a set number of elements in the array?
Jul 23 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
You cannot return an array.

You can only return a type or a pointer and an array is not a type. Rather, it is a container of some type.

However, in C++, the name of an array is the address of element 0 which makes the name of an array a pointer. - and you can return a pointer.

So if you allocate an array of int:

int* array = new int[100];

then you can return the name "array". Your function would have an int* return value.

This only works with one-dimensional arrays.

With a 2D array, the name of the array is the address of element 0 which is now also an array. You cannot return the address of an array. Again, that's because it's not the address of a type.

In this case, you use a pointer-to-pointer argument in the function.
Expand|Select|Wrap|Line Numbers
  1. void func(int (**arg)[3] )
  2. {
  3.     *arg = new int[3][3];
  4. }
  5.  
You call the function with:

Expand|Select|Wrap|Line Numbers
  1. int (*ptr)[3];                 //pointer to an array of 3 int
  2.  
  3. func(&ptr);                 //call function with address of pointer.
  4.                                  //the function will put the address of the array
  5.                                  //in the pointer
  6.  
Jul 23 '07 #3

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

Similar topics

4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
11
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway......
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
2
by: beetle | last post by:
Hello, I'm storing data in several different binary tree's. The root node is located in a struct containing general data about the tree. struct lnode { char *fname; int nentry;
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
2
by: Bowlderster | last post by:
Hi,all. I produce a function to analysis the test data,which is wave signal, and stored as array a. I want to figure out how many times it upcrosses zero,which means that when a<0,and a>0, it...
8
by: a | last post by:
Hello. Suppose I have a family of functions f_0(x) = 0*x f_1(x) = 1*x .... f_n(x) = n*x taking float and returning float (naturally, the actual functions would
19
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a href='"....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.