Unless I grossly misunderstand the way const works in a parameter definition, since argv is an array I would need to use
int main(int argc, const char const **argv)
or perhaps
int main(int argc, const char *argv[])
Apparently, then, you grossly misunderstand the way const
works in a parameter definition. We'll get to that in a moment, but first I observe that the permitted signatures for main()
are specified by the standard. Implementations are permitted to support additional signatures, but the ones that conforming, "hosted" implementations are required to support are:
int main(void)
and
int main(int argc, char *argv[])
and equivalents (C2011, 5.1.2.2.1). Regardless of all other considerations, therefore, those signatures are supported, and inequivalent ones, such as with const
qualifiers, do not need to be supported.
Back, then, to the question of array arguments. Suppose you have an array ...
int a[4];
... that you want to pass to a function. Technically speaking, you can't do it. In almost all contexts, and particularly when they appear as function arguments, array values decay to pointers. You can pass such a pointer, as is indeed common, but you cannot pass the array itself. The element type of the array, including exactly the same qualifiers, is the target type of the resulting pointer value. Moreover, values cannot be const
-- that's a property of types. Thus, it's perfectly fine to write
int a[4];
int f(int *);
f(a);
Additionally, be aware that both your proposed method signatures declare parameter argv
as a pointer to a pointer to const char
. That type is not compatible with pointer to pointer to char
. If you wanted to declare that argv
itself cannot be changed (though doing so is unnecessary) then you could write it as
int main(int argc, char ** const argv)
, which I suspect your compiler would accept. If you wanted to declare that the pointers in the argv
array cannot be changed via argv
, then that would be ...
int main(int argc, char * const *argv)
... which also stands a good chance of being accepted. What you actually tried to write, however, declared the contents of the argv
elements to be const
, and that's not compatible.