I can write
#include <stdio.h>
int main(const int argc, const char * const * const argv) {
argv = NULL;
printf("Hello, world\n");
return 0;
}
And this doesn’t compile because argv is const (which is good).
However a document I read suggested char * argv[argc + 1] as a better way to declare argv. But how can I make it so this declaration style makes argv itself const?
#include <stdio.h>
int main(const int argc, const char * const const argv[argc + 1]) {
argv = NULL;
printf("Hello, world\n");
return 0;
}
This compiles but I’d really like it not to.
how can I make it so this declaration style makes argv itself const?You shouldn't do that.argvis not const.argvis a pointer argument tomain. There's no reason why it can't be declaredconst, as long asmaindoesn't change it. That's the whole point of OP's question.argvas a pointer need not beconstand the array is defined by the standard to not beconstqualified either. See 5.1.2.2.1p2mainis a function. If it wants to restrict one of its arguments to beconst, why shouldn't it be able to? I know it doesn't have to declare itconst, but it should be possible. It actually exposes a flaw in array parameter notation in C, since the desired effect can be achieved using pointer notation, at the expense of ditching the array size info.char * argv[argc + 1]andchar * argv[]andchar ** argv. So why not stick to the last?