13

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 difference when I omit the const there?

svick
  • 225,720
  • 49
  • 378
  • 501
Kolja
  • 2,247
  • 14
  • 22

2 Answers2

13
int d(const int darg[5])

Means darg is a pointer to const int.

int e(int earg[const 5])

Means earg is a const pointer to int. This is a c99 feature. T A[qualifier-list e] is equivalent as T * qualifier-list A in the parameter declaration.

And of course (from above):

int f(const int farg[const 5])

Means farg is a const pointer to const int.

ouah
  • 138,975
  • 15
  • 262
  • 325
1

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)
haccks
  • 100,941
  • 24
  • 163
  • 252
  • 3
    `const int farg[const 5]` is adjusted to `const int * const farg` not to `const int const *farg`. – ouah Jul 08 '14 at 09:18
  • @ouah; Oopa!. That was a typo. It should be `‘‘qualified pointer to type’’` – haccks Jul 08 '14 at 09:22