473,398 Members | 2,113 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,398 software developers and data experts.

Returning an array in C++ ???

How do I do this?

I have my function:

int* calc_c2e(string it, entry ea[]) { // ..........

and in the end I want to return an integer array.

Help is much appreciated :)

Jul 22 '05 #1
3 1649

"replicat" <te******@hotmail.com> wrote in message
news:53******************************@localhost.ta lkaboutprogramming.com...
How do I do this?

I have my function:

int* calc_c2e(string it, entry ea[]) { // ..........

and in the end I want to return an integer array.


What is entry? What are you trying to achieve?
You have to be more clearer in your question.

The following demo code returns an integer array from a function -

#include <iostream>
using namespace std;
int* foo()
{
int *arr = new int[10];
for (int i=0; i<10; i++)
arr[i] = i;
return arr;
}

int main(){
int *arr=0;
arr = foo();
for (int i=0; i<10; i++)
cout << arr[i];
delete [] arr;
}

-Sharad
Jul 22 '05 #2

"replicat" <te******@hotmail.com> wrote in message
news:53******************************@localhost.ta lkaboutprogramming.com...
How do I do this?

I have my function:

int* calc_c2e(string it, entry ea[]) { // ..........

and in the end I want to return an integer array.

Help is much appreciated :)


Its impossible to return arrays in C or C++.

Your function is declared as returning a pointer. You can return a pointer
to dynamically allocated memory (which is not the same as an array, just
similar)

int* calc_c2e(string it, entry ea[])
{
...
int dynamic_memory = new int[how_many_ints];
...
return dynamic_memory;
}

But don't forget to delete[] the memory when you are done with it.

I see you are already using string, now is the time to investigate vector.
You will find it much easier than messing with dynamic memory

vector<int> calc_c2e(string it, entry ea[])

or since copying vectors might be inefficient, pass a reference to the
'returned' vector.

void calc_c2e(string it, entry ea[], vector<int>& returned_vector)

john
Jul 22 '05 #3

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...

"replicat" <te******@hotmail.com> wrote in message
news:53******************************@localhost.ta lkaboutprogramming.com...
How do I do this?

I have my function:

int* calc_c2e(string it, entry ea[]) { // ..........

and in the end I want to return an integer array.

Help is much appreciated :)


Its impossible to return arrays in C or C++.

Your function is declared as returning a pointer. You can return a

pointer to dynamically allocated memory (which is not the same as an array, just
similar)

int* calc_c2e(string it, entry ea[])
{
...
int dynamic_memory = new int[how_many_ints];
...
return dynamic_memory;
}

But don't forget to delete[] the memory when you are done with it.

I see you are already using string, now is the time to investigate vector. You will find it much easier than messing with dynamic memory

vector<int> calc_c2e(string it, entry ea[])

or since copying vectors might be inefficient, pass a reference to the
'returned' vector.


If the compiler supports return value optimization, the vector might not be
copied at all.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.