Rafaelo
Rafaelo

Reputation: 153

What does `float* const* ` mean?

Take a look at this function signature:

AudioBlock (SampleType *const *channelData, size_t numberOfChannels, size_t startSampleIndex, size_t numberOfSamples) 

from here

The main type used if float* so let's think of the signature as

AudioBlock (float *const *channelData, size_t numberOfChannels, size_t startSampleIndex, size_t numberOfSamples) 

What does float *const *channelData mean? channelData should be a const pointer to a float pointer? What is a const* something? I don't see the type of the inner pointer.

Suppose I want to create a vector of zeros so I can pass to AudioBlock:

std::vector<float> v(bufferOriginal.getNumChannels()*bufferOriginal.getNumSamples(), 0);

How do I get a float *const *channelData to this vector data?

Upvotes: 1

Views: 2959

Answers (2)

eerorika
eerorika

Reputation: 238401

What does float* const* mean?

float is a fundamental floating point type. T* is a pointer to T. const is a qualifier that applies to whatever is on the left side of it (except when it is the left most token in which case it applies to right). Since both qualifier and the pointer apply to left, it is easies to read from right to left (there are more complicated cases where this simplified rule of thumb is wrong):

float * const *  // original
* const * float  // reversed
          *          | const *          |           float // added spaces and separators
non-const pointer to | const pointer to | non-const float // translated to english

Arrays are an example of more complex cases where just right to left doesn't work. For the more complex rule that works with all compound types, see "clockwise rule" or "spiral rule".

So it's not possible to get a float* const * to the vector data then, right?

You could, if you had a vector like this:

std::vector<float*> vector_of_pointers;
float* const* ptr = vector_of_pointers.data();

You could make element of that vector point to your vector of floats.

vector_of_pointers.push_back(v.data());

Upvotes: 3

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

This parameter declaration

float *const *channelData

means that the variable channelData (read from tight to left) is a pointer to a constant pointer (the pointer itself that is constant) to a non-constant object of the type float.

To make it more clear consider the following demonstrative program.

#include <stdio.h>

int main(void) 
{
    float x = 1.1f;
    float y = 2.2f;
    
    printf( "x = %f\n", x );

    float * const px = &x;
    
//  px = &y;    //  error: assignment of read-only variable ‘px’
    *px = y;
    
    printf( "x = %f\n", x );
    
    float * const * ppx = &px;
    
//  *ppx = &y;  //  error: assignment of read-only location ‘*ppx’

    **ppx = 3.0f;
    
    printf( "x = %f\n", x );

    return 0;
}

Its output is

x = 1.100000
x = 2.200000
x = 3.000000

As you can see as the pointer px is a constant variable it has to be initialized when it is declared. You can not change it such a way that it will point to another variable. But you can use pc to change the value of the object pointed to by px. The variable ppx is a pointer to such a pointer.

Upvotes: 0

Related Questions