# Returning pointers from Juce's Array

**URL:** <https://forum.juce.com/t/returning-pointers-from-juces-array/15493>\
**Category:** General JUCE discussion\
**Created:** [September 8, 2015, 8:12am UTC](https://forum.juce.com/t/returning-pointers-from-juces-array/15493 "2015-09-08T08:12:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![swdpowell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swdpowell/32/13_2.png) [@swdpowell](https://forum.juce.com/u/swdpowell)\
**Post date:** [September 8, 2015, 8:12am UTC](https://forum.juce.com/t/returning-pointers-from-juces-array/15493/1 "2015-09-08T08:12:16Z")

</div>

Hello all.

Using the Juce Array, I have an Array of an Array of doubles.

If I call .getFirst() on the first Array of doubles I get the correct result, the first value of the array:

floatTester = midiTrackNotesArray[0].getFirst();

However, I need the pointer to return a pointer to the&nbsp;Array of Arrays from a function, so I use begin to return a pointer to the first Array:

tempArrayPointer = midiTrackNotesArray[0].begin();

Surely, that pointer points to the value which getFirst() returns correctly. However, when I dereference the pointer:

floatTester = \*tempArrayPointer;

I get a nonsense return: e.g. -1.45682e+144.

I have tried plenty of variations but to no avail. Apologies if this is a C++ question, as I'm learning both C++ and library together it's not always clear which side of the fence the questions falls, but as I was using the Juce Array object it seemed to make sense.

Thank you.

&nbsp;

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 8, 2015, 8:22am UTC](https://forum.juce.com/t/returning-pointers-from-juces-array/15493/2 "2015-09-08T08:22:21Z")

</div>

Most likely you're using the pointer after the original Array has been deleted.

---

<div class="post-metadata">

**Author:** ![swdpowell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swdpowell/32/13_2.png) [@swdpowell](https://forum.juce.com/u/swdpowell)\
**Post date:** [September 8, 2015, 8:27am UTC](https://forum.juce.com/t/returning-pointers-from-juces-array/15493/3 "2015-09-08T08:27:05Z")

</div>

Hello Jules, thank you for your quick reply.

The code follows straight on from each other, so I can't imagine it would go out of scope:

&nbsp; &nbsp; &nbsp; &nbsp; midiTrackNotesArray.insert(i, tempNoteArray);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; tempArrayPointer = midiTrackNotesArray[0].begin();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; floatTester = \*tempArrayPointer;  
&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 8, 2015, 8:51am UTC](https://forum.juce.com/t/returning-pointers-from-juces-array/15493/4 "2015-09-08T08:51:19Z")

</div>

It is going out of scope - the operator[] returns a temporary copy of the object you ask for. You probably want to use getReference(0) instead.

---

<div class="post-metadata">

**Author:** ![swdpowell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swdpowell/32/13_2.png) [@swdpowell](https://forum.juce.com/u/swdpowell)\
**Post date:** [September 8, 2015, 9:15am UTC](https://forum.juce.com/t/returning-pointers-from-juces-array/15493/5 "2015-09-08T09:15:19Z")

</div>

That works perfectly, so now I have the direct reference, I can create a pointer to that and return that from the function.

Thank you very much, many things (well, every step at the moment!) comes slow and I work most of them out, sometimes things just don't mentally unstick!
