我已经问过这个问题并且做了一个非常糟糕的工作,导致了令人困惑的答案,这些答案无法解决我的问题。这一次我要努力让我的问题更容易解决。这是下面的问题......
int builtin_cmd(char **argv);
int main()
{
char *argv[128]; //create an array of c_strings with 128 slots in the array
//put 'quit' into argv[0].. (This was done by another file given to me by the professor)
printf("%s\n", argv[0]); //prints 'quit', as it should
builtin_cmd(argv); //call builtin_cmd and pass argv to the function
}
int builtin_cmd(char **argv)
{
printf("%s\n", argv[0]); //prints nothing, should have printed 'quit'
}
基本上我遇到的问题是在函数char *argv[128]
中访问builtin_cmd
。我将数组中的第一个索引设置为c_string'quit'并在那里打印它就像我预期的那样,printf()在终端中打印出'quit'。但是,一旦我调用builtin_cmd(argv);
,行printf("%s\n", argv[0]);
就会完全打印出来,就好像argv是空的一样。我该如何正确地将argv传递给函数builtin_cmd?