const for array size expressions on argument

前端 未结 2 1349
长发绾君心
长发绾君心 2021-02-19 16:56

I have the following C code example:

int f(const int farg[const 5])
{
}

What does the additional const for the array size do? And what is the d

2条回答
  •  死守一世寂寞
    2021-02-19 17:09

    What does the additional const for the array size do?

    C11: 6.7.6.3:

    A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation.

    This means

    int f(const int farg[const 5])  
    

    will be adjusted to

    int f(const int *const farg)  
    

    And what is the difference when I omit the const there?

    After omitting, it is equivalent to

    int f(const int frag[5])  //or int f(const int frag[])
    

    which is ultimately equivalent to

    int f(const int *farg)
    

提交回复
热议问题