1#include <iostream>
2using namespace std;
3
4int* fun()
5{
6 int* arr = new int[100];
7
8 /* Some operations on arr[] */
9 arr[0] = 10;
10 arr[1] = 20;
11
12 return arr;
13}
14
15int main()
16{
17 int* ptr = fun();
18 cout << ptr[0] << " " << ptr[1];
19 return 0;
20}
1int * fillarr(int arr[], int length){
2 for (int i = 0; i < length; ++i){
3 // arr[i] = ? // do what you want to do here
4 }
5 return arr;
6}
7
8// then where you want to use it.
9int main(){
10int arr[5];
11int *arr2;
12
13arr2 = fillarr(arr, 5);
14
15}
16// at this point, arr & arr2 are basically the same, just slightly
17// different types. You can cast arr to a (char*) and it'll be the same.
18
1#include <iostream>
2#include <ctime>
3
4using namespace std;
5
6// function to generate and retrun random numbers.
7int * getRandom( ) {
8
9 static int r[10];
10
11 // set the seed
12 srand( (unsigned)time( NULL ) );
13
14 for (int i = 0; i < 10; ++i) {
15 r[i] = rand();
16 cout << r[i] << endl;
17 }
18
19 return r;
20}
21
22// main function to call above defined function.
23int main () {
24
25 // a pointer to an int.
26 int *p;
27
28 p = getRandom();
29
30 for ( int i = 0; i < 10; i++ ) {
31 cout << "*(p + " << i << ") : ";
32 cout << *(p + i) << endl;
33 }
34
35 return 0;
36}
1string* getNames() {
2 string* names = new string[3];
3 names[0] = "Simon";
4 names[1] = "Peter";
5 names[2] = "Dave";
6
7 return names;
8}
9