Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Returning an Array from a Function

C++ Returning an Array from a Function

C++ Arrays C++ Arrays

C++ does not allow returning a complete array as a function parameter. However, you can return a pointer to an array by specifying the array name without an index.

If you want to return a one-dimensional array from a function, you must declare a function that returns a pointer, as follows:

int

myFunction

(

)

{

//. . .

}

The following example:

Example

int

myFunction

(

)

{

   

int

myArray

[

3

]

=

{

1

,

2

,

3

}

;

   

return

myArray

;

}

**Note:** You cannot simply return a pointer to a local array, because when the function ends, the local array will be destroyed and the pointer to it will become invalid.

C++ does not support returning the address of a local variable outside the function, unless the local variable is defined as static variable.

To avoid the above situation, you can use a static array or a dynamically allocated array.

Using a static array requires creating a static array inside the function and returning its address, for example:

int

myFunction

(

)

{

   

static

int

myArray

[

3

]

=

{

1

,

2

,

3

}

;

   

return

myArray

;

}

Now, let us look at the function below, which generates 10 random numbers and returns them using an array, as follows:

Example

#include

<

iostream

#include

<

cstdlib

#include

<

ctime

using

namespace

std

;

// Function to generate and return random numbers

int

getRandom

(

)

{

static

int

r

[

10

]

;

// Set the seed

srand

(

(

unsigned

)

time

(

NULL

)

)

;

for

(

int

i

=

0

;

i

<

10

; ++

i

)

{

r

[

i

]

=

rand

(

)

;

cout

<<

r

[

i

]

<<

endl

;

}

return

r

;

}

// Main function to call the function defined above

int

main

(

)

{

// A pointer to an integer

int

p

;

p

=

getRandom

(

)

;

for

(

int

i

=

0

;

i

<

10

;

i

++

)

{

cout

<<

"

*(p +

"

<<

i

<<

"

) :

"

;

cout

<< *

(

p

i

)

<<

endl

;

}

return

0

;

}

When the code above is compiled and executed, it produces the following result:



624723190

1468735695

807113585

976495677

613357504

1377296355

1530315259

1778906708

1820354158

667126415

*(p + 0) : 624723190

*(p + 1) : 1468735695

*(p + 2) : 807113585

*(p + 3) : 976495677

*(p + 4) : 613357504

*(p + 5) : 1377296355

*(p + 6) : 1530315259

*(p + 7) : 1778906708

*(p + 8) : 1820354158

*(p + 9) : 667126415

Using a dynamically allocated array requires using the new operator to allocate an array inside the function, and using the delete operator to free the array when the function ends, for example:

Example

int

myFunction

(

)

{

   

int

myArray

=

new

int

[

3

]

;

   myArray

[

0

]

=

1

;

   myArray

[

1

]

=

2

;

   myArray

[

2

]

=

3

;

   

return

myArray

;

}

int

main

(

)

{

   

int

result

=

myFunction

(

)

;

   

// Use result

   

delete

[

]

result

;

   

return

0

;

}

The following is a simple application example demonstrating how to return an array from a function:

Example

#include

using

namespace

std

;

int

createArray

(

int

size

)

{

   

int

arr

=

new

int

[

size

]

;

   

for

(

int

i

=

0

;

i

<

size

;

i

++

)

{

        arr

[

i

]

=

i

1

;

   

}

   

return

arr

;

}

int

main

(

)

{

   

int

myArray

=

createArray

(

5

)

;

   

for

(

int

i

=

0

;

i

<

5

;

i

++

)

{

       

cout

<<

myArray

[

i

]

<<

" "

;

   

}

   

cout

<<

endl

;

   

delete

[

]

myArray

;

// Free memory

   

return

0

;

}

In the example above, we declared a function named createArray that accepts an integer parameter size and returns an integer array filled with integers. We used the new operator to dynamically allocate an array on the heap and filled the array inside the function. Finally, the function returned a pointer to the array.

In the main function, we called the createArray function and stored the returned array pointer in myArray. Then we iterated over the array and printed the value of each element. Finally, we used the delete[] operator to free the memory occupied by myArray to avoid memory leaks.

When the code above is compiled and executed, it produces the following result:

1 2 3 4 5 

**Note:** When using a dynamically allocated array, the code that calls the function is responsible for freeing the returned array. This is because the array allocated inside the function is not automatically freed when the function ends.

C++ Arrays C++ Arrays