Returning an array in C++ ???

R

replicat

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 :)
 
S

Sharad Kala

replicat said:
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;
return arr;
}

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

-Sharad
 
J

John Harrison

replicat said:
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
 
P

Peter van Merkerk

John Harrison said:
news:5398296986cdd4ac493c3d7fff7caa11@localhost.talkaboutprogramming.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top