Objective-c 主程序,什么是:int argc, const char * argv[] [英] Objective-c main routine, what is: int argc, const char * argv[]
问题描述
传递给命令行程序的 main 方法的参数是什么:
What are the arguments passed into the main method of a command-line program:
int main(int argc, const char * argv[])
第一个 int 是什么意思?
what is the first int mean?
第二个参数是什么,是一个字符数组?
And what is the 2nd parameter, is that an array of chars?
如何使用这些?
另外,命令行项目类型的实际用途是什么,除了用来学习 obj-c 即练习.
Also, what practical use is a command-line project type, other than using it to learn obj-c i.e. to practise.
推荐答案
argc
表示参数计数".它表示有多少参数被传递到可执行文件中.argv
表示参数值".它是一个指向字符数组的指针.或者换个角度考虑,它是一个C字符串数组(因为C字符串只是字符数组).
argc
means "argument count". It signifies how many arguments are being passed into the executable.
argv
means "argument values". It is a pointer to an array of characters. Or to think about it in another way, it is an array of C strings (since C strings are just arrays of characters).
所以如果你有一个程序foo"并像这样执行它:
So if you have a program "foo" and execute it like this:
foo -bar baz -theAnswer 42
然后在您的 main()
函数中,argc
将是 5,而 argv
将是:
Then in your main()
function, argc
will be 5, and argv
will be:
argv[0] = "/full/path/to/foo";
argv[1] = "-bar";
argv[2] = "baz";
argv[3] = "-theAnswer";
argv[4] = "42";
这篇关于Objective-c 主程序,什么是:int argc, const char * argv[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!