开发者

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 chars.

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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜