What is an array of constant pointers in C?

arrays, c, constants, function, pointers

Solution

No, the `const` in `char *const argv[]` is not redundant.

First, `const` and "constant" are actually two different things in C, even though the `const` keyword is obviously derived from the word "constant". A constant expression is one that can be evaluated at compile time. `const` really means "read-only". For example:

const int r = rand();

is perfectly legal.

Yes, the address of an array -- like the address of any object -- is read-only. But that doesn't mean that the value of the array (which consists of the values of its elements) is read-only, any more than any other object is necessarily read-only.

Consider these three declarations:

char *arr1[10];
char *const arr2[10];
const char *arr3[10];

`arr1` is a 10-element array of pointers to `char`. You can modify the `char*` elements and you can modify the objects that those elements point to.

`arr2` is an array of `const` (read-only) pointers to `char`. That means that you can't modify the `char*` elements of the array (once they're initialized) -- but you can still modify the `char` objects or arrays that those elements point to.

And `arr3` is an array of pointers to `const char`; you can modify the array elements, but you can't modify what they point to.

Now the fact that you used the name `argv` suggests that you're talking about the second parameter to `main`, which has some huge effects on this. The language specifies that `main`'s second parameter is

char *argv[]

or, equivalently,

char **argv

There is no `const`. You can probably get away with adding one, but it's best to follow the form specified by the standard. (Update: I see from your comment that you're asking about the `argv` parameter of `getopt()`, which is defined as `char * const argv[]`.)

And since it's a parameter defined as an array, another rule comes into play: a parameter defined as an array of some type is "adjusted" to a pointer to that type. (This rule applies only to parameters.) This isn't a run-time conversion. A function cannot have a parameter of array type.

The relationship between arrays and pointers in C can be confusing -- and there's a lot of misinformation out there. The most important thing to remember is that arrays are not pointers.

Section 6 of the comp.lang.c FAQ is an excellent explanation of the details.

Problem

Isn't the address of an array and thus of all its elements as well constant anyway? And if so, in a declaration like: ``` char *const argv[] ``` isn't the `const` qualifier redundant?

Original source