Why argv[] is declared as const?
In Objective-C, why argv[] is declar开发者_开发百科ed as const:
int main(int argc, const char *argv[]) { }
Because const
denotes that the value (char *
) is immutable, which arguments are.
Once program arguments are handed off to a program, their values should not be modifiable.
Think of this array as an array of const char *
's, which in turn, is an array of char
s.
So, say you passed the string "hello world" to your program as arguments, argv
would look like this:
{{'h', 'e', 'l', 'l', 'o', '\0'}, {'w', 'o', 'r', 'l', 'd', '\0'}}
Because it makes writing the shell/program launcher/etc simpler while not making most programs much more complicated.
And the creators of 'C' were thinking ahead to make OS writers jobs easier - especially their own!
精彩评论