Handmade Hero»Forums»Code»How do i pass an array to a pointer ?
Gavin Williams
26 posts
How do i pass an array to a pointer ?
5 years, 3 months ago Edited by Gavin Williams on Feb. 5, 2015, 6:37 p.m.
In my XAudio code I have my buffer data, im trying to get my buffer data into the fillbuffer function, but it doesn't work. Here's all the relevant code...

int16* bufferData;
bufferData = new int16[buffer.AudioBytes/2];
buffer.pAudioData = (const byte*)bufferData[0];
void DebugFillBufferWithSineWave()
int16* sampleOut = (int16*)&(buffer.pAudioData);

But this and every other variation crash. One variation i got some of the sound to play, but only if i set the sample count to half the actual sample count. Can a C++ person please help.

Edit: I've changed the code a bit, I'm pretty sure the above is just wrong, it now reads ...

buffer.pAudioData = (const byte*)&bufferData[0];
int16* sampleOut = (int16*)&bufferData[0];

And one the problems turned out to be the double *sampleOut++, as i was using mono.
Mārtiņš Možeiko
2075 posts / 1 project
How do i pass an array to a pointer ?
5 years, 3 months ago
What do you mean with "pass an array to a pointer"? If you are asking how to assing array to pointer, then you do that simply with assignment "=" operator. Arrays also are pointers, so there is nothing to convert:
int array[100];
int* pointer = array; // now pointer points to first element of array
pointer = array + 9; // now pointer points to 10th element of array
pointer = &array[9]; // same as line above, but different syntax

If something crashes - examine that in debugger. What line it crashes, what it code is doing on that line (reading memory, writing memory, etc). What are values of variables. And from that information deduce why it is crashing and fix it.