Search code examples
carraysvolatilefunction-parameter

What is the purpose of the "volatile" keyword appearing inside an array subscript?


While I was browsing cppreference, I saw a strange type array in function parameters like this:

void f(double x[volatile], const double y[volatile]);

So, What is the purpose of the volatile keyword appearing inside an array subscript? What does it do?


Solution

  • The volatile keyword is used to declare an array type of a function parameter.

    Here, double x[volatile] is equivalent to double * volatile x.

    The cppreference says :

    In a function declaration, the keyword volatile may appear inside the square brackets that are used to declare an array type of a function parameter. It qualifies the pointer type to which the array type is transformed. The following two declarations declare the same function:

    void f(double x[volatile], const double y[volatile]);
    
    void f(double * volatile x, const double * volatile y);
    

    This syntax only valid in C language in function parameters.