我目前正试图让我的C程序读取来自用户的Unix参数。我到目前为止搜索了这个网站,但我一直无法弄清楚我做错了什么 - 虽然我承认我的指针实现技能是相当有限的。C - 使用execvp和用户输入
以下是我现在的代码;我一直在与没有运气的指针搞混了。错误也是说我需要使用const * char,但是我在其他例子中看到* char可以由用户输入。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
main()
{
char args[128];
//User input
printf("> ");
fgets(args, 128, stdin);
execvp(args[0], *args[0]);
}
我得到的错误如下:
smallshellfile.c: In function ‘main’:
smallshellfile.c:13:21: error: invalid type argument of unary ‘*’ (have ‘int’)
smallshellfile.c:13:5: warning: passing argument 1 of ‘execvp’ makes pointer from integer without a cast [enabled by default]
/usr/include/unistd.h:575:12: note: expected ‘const char *’ but argument is of type ‘char’
有谁知道这个问题可能是什么?
不要忘记,命令名在'argv [0]'中出现,在'argv [1]'中出现第一个参数,在'argv [2]'中出现空指针。 – 2012-02-14 00:19:14
@JonathanLeffler如果我正确地阅读了这个问题,他不想传递任何参数(程序名除外),所以NULL应该在'argv [1]'中。 – asaelr 2012-02-14 00:24:09
[0]只是因为我用完了想法。我摆脱了他们,并做出了修改 - 我编译时的错误消失了,但执行程序在被调用时不会执行任何操作 - 即如果我尝试ls,它只会终止程序。 – limasxgoesto0 2012-02-14 00:25:13