I tried assigning the argv value to a constant pointer, believing that it's safe to assign a non-const to a const variable.
Unfortunately gcc complained:
> make CFLAGS=-Wall mein
cc -Wall mein.c -o mein
mein.c: In function ‘main’:
mein.c:5:30: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
const char *const*const a = argv + 1;
^~~~
mein.c:5:26: warning: unused variable ‘a’ [-Wunused-variable]
const char *const*const a = argv + 1;
The corresponding C source looks like this:
2021-07-08 13:49 mein.c Page 1
1 #include <stdlib.h>
2
3 int main(int argc, char *argv[])
4 {
5 const char *const*const a = argv + 1;
6 return 0;
7 }
I believe my declaration says a is a constant pointer to an array of constant pointers pointing to constant strings.
I also believe that char *argv[] is equivalent to char **argv.
The question is: Am I wrong, or is gcc wrong?
For initialization, C 2018 6.7.9 11 says:
For simple assignment, 6.5.16.1 1 says:
For this purpose, the “left operand” is the
awith typeconst char * const * constand the “right operand” isargv + 1with typechar **. The type the left operand points to isconst char * const, and the type the right operand points to ischar *.The left operand is a qualified version of
const char *, and the right operand is an unqualified version ofchar *. Then this initialization conforms to the constraints if the former type (const char *) is compatible with the latter (char *).6.7.6.1 2 says:
So
const char *andchar *are compatible ifconst charandcharare compatible.6.7.3 11 says:
Since
const charisconstqualified andcharis not, these are not compatible types.Therefore, this initialization does not conform to the constraints of the C standard.
GCC is right, you are wrong.