return an array from a function

I am new in C++
This is my code below:
the result of my function lancerDe is an array TabDes. I want the data to return to the main of my program. is it possible

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int lancerDe()
{
int TabDes[10];
srand ( time(NULL) );
for (int i=0;i<10;i++)
{
TabDes[i]= rand() % 6 + 1;
cout << TabDes[i] << endl;
}

}


int main()
{


int Table[10];
char reponse;
reponse = 'o' ;
int counter = 0;
while (reponse == 'o')
{
if (counter == 0)
{
cout <<"Voulez-vous lancer un de (o/n) ? ";
cin >> reponse ;
counter++;
}
else
{
cout <<"Voulez-vous rejouer (o/n) ? ";
cin >> reponse ;
}
cout <<"Les faces obtenues a chaque lancer de de sont :" << endl;
lancerDe();


}
Yes, but you would have to allocate the array using new and use delete on the pointer at the end of main. Also, make lancerDe() return an int* instead of an int (it's an array not a single integer).
I don't know how . can you show me
thanks
Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
int* makeArray(unsigned int size) {
    if(size == 0) throw std::exception("I don't feel like handling this");
    return new int[size];
}

int main() {
    int* array = makeArray(5);
    for(unsigned int i = 0; i < 5; ++i) array[i] = 2*i;
    for(unsigned int i = 0; i < 5; ++i) std::cout<<array[i]<<std::endl;
    delete array; //remember to delete it, since you are the only one with a pointer to it
    return 0;
}
Last edited on
don't understand your example but thanks anyway
Alright, I don't know much about C++...I'm still learning the language so idk what the HECK a throw or catch or exception is, but I do know that when you're returning arrays, you CAN'T. You can return a pointer to an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include<cmath>
int * that_array_function(void);
int main(void)
{
...
int * arr = that_array_function();
return 0;
}
int * that_array_function(void);
...
int b[x] //where x is the size
return b //or return * b or something... 
For fun only, it doe not return an array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <ctime>

using std::cout;
using std::endl;
using std::cin;

const size_t Index = 10;
void lancerDe(int TabDes[Index])						
{
	srand(time(0) );
	for (size_t i = 0; i < Index; i++)
	{
		TabDes[i]= rand() % 6 + 1;
	}
}
int main() {
	int* TabDes = new int[Index];
	lancerDe(TabDes);
	for (size_t i = 0; i < 10; i++)
	{
		cout << TabDes[i] << endl;
	}
	delete TabDes;

	return 0;
}


This one does return an array, but not safe:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <ctime>

using std::cout;
using std::endl;
using std::cin;
const size_t Index = 10;

int* lancerDe()						
{
	int* TabDes = new int[Index];
	srand(time(0) );

	for (size_t i = 0; i < Index; i++)
	{
		TabDes[i]= rand() % 6 + 1;
	}
	return TabDes;
}

int main() {
	int* result = lancerDe();
	for (size_t i = 0; i < Index; i++)
	{
		cout << result[i] << endl;
	}
        delete result; // how can we remember this?
}
Last edited on
http://cplusplus.com/reference/stl/vector/

I don't see any reason to ever return an array from a function. Pass it as an out parameter and have a function fill it with values.
Take a look at this also.
http://cplusplus.com/reference/algorithm/generate/

Simply fill the RandomNumber () predicate function shown in the example with
return (rand() % 6 + 1);

Using the rest of the example you can see how you can use a predicate to fill your vector. Then there are no worries about memory leaks!
Topic archived. No new replies allowed.