Noob Pointer Question getWritePointer

Hi All

Sorry, python programmer so the nuances of C++ are still new to me. Quick question about pointers. in particular. In the following code buffer is set as a pointer [1] to what I assume is the start sample of the output channel buffer.

        // Get a pointer to the start sample in the buffer for this audio output channel
        auto* buffer = bufferToFill.buffer->getWritePointer (channel, bufferToFill.startSample); //[1]

        // Fill the required number of samples with noise between -0.125 and +0.125
        for (auto sample = 0; sample < bufferToFill.numSamples; ++sample)
            buffer[sample] = random.nextFloat() * 0.25f - 0.125f; //[2]

I understand that to be an address but, why don’t I have to dereference it later [2]?
I would have expected that line to look like :
*(buffer+sample) = random.nextFloat() * 0.25f - 0.125f; //[2]

I am only new some simple explanation would be great :smiley:



You do dereference it, with the subscript operator. In C and C++, arrays decay to pointers. This means that if you pass, for example, a float [150] as an argument or return, it actually passes as float*, losing the dimension information. In this case, you have a pointer to an array -the samples in the buffer. *buffer is then equivalent to buffer[0], *(buffer + 1) to buffer[1] and so on.

1 Like

Ah, that makes sense. Thanks very much for explaining